我正在开发一个插件,在插件激活时将内容插入wordpress。我还有一个管理员,我可以在那里添加一个链接到插入的帖子。管理员是一个输入框,我可以在这里输入我的链接并单击保存,它应该在帖子中更新。
基本上,插件可以在激活后插入帖子,将链接保存在管理区域,但问题是,当我在管理区域输入帖子时,链接在帖子中没有更新。我认为这是其中一个已经发送的标题问题,因为注册激活挂钩在管理设置更新之前运行。谁能给我指一下正确的方向吗。
我的管理页面如下所示。
function link_input(){
global $options. this is the global function that hold the setting. i.e $options = get_option(my_settings);
ob_start();// starts php output buffer.
?>
<form action ="options.php" method="POST"><?php echo settings_fields(\'link_group\');?>
<input type="text" name="my_settings[\'my_link\']" value="<?php echo options[\'my_link\'];?>"/>
<input type="button" class="button-primary" value="save link"/>
</form>
<?php
echo ob_get_clean();
}
这是在插件激活时插入post的函数。
function insert_post_on_activation(){
global $options; // this is the link that I enter in the admin area. Set to global.
// Create post object
$my_post = array(
\'post_title\' => \'Test Post\',
\'post_content\' => \'This is my post. <a href="\'.$options[\'my_link\'].\'">Link</a>\',/*this is the link that is not reflecting what I insert in the admin are */
\'post_status\' => \'publish\',
\'post_author\' => 1
);
// Insert the post into the database
wp_insert_post( $my_post );
}
register_activation_hook(__FILE__, \'insert_post_on_activation\');
最合适的回答,由SO网友:Azhar Munshi 整理而成
register_activation_hook
仅在插件处于活动状态时激发。
您应该在中使用您的函数add_action(\'admin_init\',\'your_function_name\')
.