I am developing a plugin that inserts content to wordpress on plugin activation. I also have an admin are where I can add a link to the inserted post. The admin is a input box where I can input my link and click save and it should be updated in the post.

Basically the plugin works as far as inserting post when it's activated, saving the link in the admin area, but the problem is that the link is not updated in the post when I enter one in the admin area. I believe this is one of those headers already sent issues since register activation hook runs before the admin setting is updated. Can someone point me to the right direction.

My admin page look like this.

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"/>

echo ob_get_clean();

This is the function that inserts post on plugin activation.

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');
  • 2
    uhmm...register_activation_hook only fires when plugin is first activated. You need to handle your post submit some other way to get your function to run when the form is submitted.
    – Will
    Dec 14 '13 at 5:11
  • Where is the handler for that form submission?
    – s_ha_dum
    Dec 14 '13 at 5:15

register_activation_hook only fires when plugin is going to active.

You should use your function in add_action('admin_init','your_function_name').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.