简体   繁体   中英

Moving function from functions.php to plugin

I'm probably doing something stupid.

But i have the following working code, (this a small (but working) part of the code) if i have this code in my functions.php it works fine, but when i add it in a custom plugin, it does not

function update_booklink_field( $post_id ) {
if( ! ( wp_is_post_revision( $post_id) || wp_is_post_autosave( $post_id ) ) ) {

update_post_meta( $post_id, 'prijstest', 'testvalue' ); 
}
}
add_action( 'save_post', 'update_booklink_field' );

This is my code in the plugin, is there something obvious i am missing? Or are there extra steps i need to take to trigger a function when it is in a plugin?

<?php
/**
 * Plugin Name: aaautofill
*/

  function update_booklink_field( $post_id ) {
    if( ! ( wp_is_post_revision( $post_id) || wp_is_post_autosave( $post_id ) ) ) {

    update_post_meta( $post_id, 'prijstest', 'testvalue' ); 
    }
    }
    add_action( 'save_post', 'update_booklink_field' );






?>

EDIT:

What i found out so far: if i move

add_action( 'save_post', 'update_booklink_field');

to the functions file, then it works. The plugin is activated, but the save_post just doesnt run when its in my plugin file. i really have 0 clue what is causing this :/

edit 2:

Basicly the function doesnt seem to have an issue, because if i change the location of when its loaded (in the fucntion.php) it works fine.

  1. Issue is the add_action save hook (probably) because
  2. plugin is live/active/works (tested with var dumbs)
  3. the function also works, if the add_action is in the functions.php vs when it's in the plugin file.
    1. no other plugins are live, default twenty seventeen, theme

Does this mean it has to do with the order in which the things are loaded? (or am i overlooking something stupid?)

Edit 3, i found it/got it working if i changed the save_post line to:

add_action( 'save_post', 'update_booklink_field', 50 , 50 );

it works

Perhaps this will do the trick:

<?php
/**
* Plugin Name: aaautofill
*/

function update_booklink_field( ) {

global $post;

if( ! ( wp_is_post_revision( $post->ID ) || wp_is_post_autosave( $post->ID ) ) ) {

 update_post_meta( $post->ID, 'prijstest', 'testvalue' ); 
}
}
add_action( 'save_post', 'update_booklink_field' );

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM