dtkyayvldeaqhl7151 2018-11-13 16:01
浏览 34

将函数从functions.php移动到插件

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

  • 写回答

1条回答 默认 最新

  • dpevsxjn809817 2018-11-13 17:23
    关注

    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' );
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单