0

I want to display a custom button in my "Edit post" screen which saves the current post (everything including custom fields) and then runs a function (in my case redirecting to another page).

I know how to redirect after saving the post using the save_posts action and wp_redirect but I can't figure out how to save the post when a custom button is clicked.

This is my function for redirecting using save_post (which doesn't work for me since I need to use a custom button):

function redirect_after_save_post() {
  global $post;

  if ( 'event' == get_post_type() ) {
    $url = 'http://www.google.com';
    wp_redirect( $url );
    exit;
  }
}

add_action( 'save_post', 'redirect_after_save_post' );
0

I think you inject some code to display your custom button.

You can try to add the save post action in your code:

do_action('save_post');

And then add redirect code, be carefull with the Infinite loop.

0

Here's what I do. It doesn't create a new button, but will put a check mark right next to the publish/update button. The idea is that if you check the button before hitting the publish/update. it will run your function in the save. if it isn't check the system will save as normal. I have generalized the function names so you can make them more specific as well as button titles.

function rt_custom_save_action(){
    global $post;
    if (get_post_type($post) != 'post') //Choose Post type HERE
        return false;

        $html  = '<div id="major-publishing-actions" style="overflow:hidden">';
        $html .= '<div id="publishing-action">';
        $html .= '<label><input type="checkbox" value="1" name="custom_action" />SAVE WITH FUNCTION</label>';
        $html .= '</div>';
        $html .= '</div>';
        echo $html;
}
add_action( 'post_submitbox_misc_actions', 'rt_custom_save_action' );

function rt_project_updated_save_dosomething( $post_id ) {


    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return;
    // If this is just a revision, don't do anything.
    //if ( wp_is_post_revision( $post_id ) )
    //  return;

    // only go if user checked action checkbox.

    if(isset($_POST['custom_action'])){

    //PUT SOMETHING HERE THAT YOU WANT TO HAPPEN

    }
}
add_action( 'save_post', 'rt_project_updated_save_dosomething' ); //if you change the custom post type above, you have to change it here too (save_post_CUSTOMPOSTNAME)

If you wanted to use a button i suppose you could do this, i've removed the checkbox check in the function and added 2 button options. Try one then the other:

function rt_custom_save_action(){
    global $post;
    if (get_post_type($post) != 'post') //Choose Post type HERE
        return false;

        $html  = '<div id="major-publishing-actions" style="overflow:hidden">';
        $html .= '<div id="publishing-action">';
       // $html .= '<input type="submit" accesskey="p" tabindex="5" value="custom_action" class="button-primary" id="custom" name="custom_action" value="submitted">';
      //try this button code first.
      //$html .= '<button onclick="rt_project_updated_save_dosomething( $post_id );" accesskey="p" tabindex="5" class="button-primary" id="custom" name="custom_action">SEND WITH FUNCTION</button>';
      //try this one next but don't use both.
        $html .= '</div>';
        $html .= '</div>';
        echo $html;
}
add_action( 'post_submitbox_misc_actions', 'rt_custom_save_action' );

function rt_project_updated_save_dosomething( $post_id ) {


    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return;
    // If this is just a revision, don't do anything.
    if ( wp_is_post_revision( $post_id ) )
        return;

    //PUT SOMETHING HERE THAT YOU WANT TO HAPPEN

}
add_action( 'save_post', 'rt_project_updated_save_dosomething' ); //if you change the custom post type above, you have to change it here too (save_post_CUSTOMPOSTNAME)

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.