Add Publish/Unpublish button to quicklinks

I’m having a hard time making this function and wonder if you can help.

In the quick link line under each post title (in the backend), I’d like to add a PUBLISH link. Pressing it publishes the post (if it is set at draft) or un-publishes it (if it is set at published). Do you know what the function would be to add such functionality? I can’t find anything online about it and it seems like such a simple thing.

Thanks! Mike

  • Vaughan
    • Support/SLS MockingJay

    Hi Mike,

    Hope you’re well?

    It wouldn’t be easy I don’t think.

    there’s a couple of hooks that I know of, 1 of whichwill add a button or link just above the title form (below the Edit Post title) and 1 which will allow you toadd buttons to the media buttons. ie, next to the buttons for adding media to the post.

    // adds custom link/button below the edit post title
    add_action( 'edit_form_top', 'wpmudev_top_form_edit_button' );
    function wpmudev_top_form_edit_button( $post ) {
    echo "<a href='#' id='custom_publish_button' class='button custom_publish_button'>Publish</a>";
    }

    the other action to add where the media buttons are is:

    add_action( 'media_buttons', 'add_form_button' );

    So far I have come up with the following custom function.

    add_action( 'edit_form_top', 'wpmudev_top_form_edit_button' );
    function wpmudev_top_form_edit_button( $post ) {

    $status = get_post_status( $post->ID );
    if('publish' == $status) {
    echo '<a href="#" id="custom_publish_button" class="button custom_publish_button">Un-Publish</a>';
    //change_post_status($post_id, 'draft');
    } elseif ('draft' == $status) {
    echo '<a href="#" id="custom_publish_button" class="button custom_publish_button">Publish</a>';
    //change_post_status($post_id, 'publish');
    }
    }

    function change_post_status($post_id, $status){
    $current_post = get_post( $post_id, 'ARRAY_A' );
    $current_post['post_status'] = $status;
    wp_update_post($current_post);
    }

    Please be aware though, this is not complete, but it will get you the button displayed. It requires some javascript or ajax which will call the change_post_status() function when the button is clicked.

    If you are able to finish that :slight_smile: otherwise I can spend a bit more time and get it fully working if you are unable to finish it yourself.

    Hope this helps

  • Mike
    • Flash Drive

    Thanks so much! The toolbar publish isn’t what I’m looking for because that requires you to open the post to unpublish or publish. If I’m already there, I might as well use the normal wordpress way. I’d like for people to be able to publish and unpublish just from the posts.php page that lists all the posts. It’s such a pain to have to open each to do that. I can’t see why wordpress does that. Quick edit I guess is a faster way but I want even faster… having a link next to each post to publish/unpublish. I’m talking about here: http://screencast.com/t/Xrldq0HOE

  • Mike
    • Flash Drive

    The code thing you sent is pretty cool and is pretty close to what I am looking for. It does add the column but when I click on the word PUBLISHED, nothing happens. Has something changed since 2011 when written? :slight_smile: So close!

  • Mike
    • Flash Drive

    Whoa… that’s EXACTLY what I wanted!! THANK YOU! This should be core wordpress!!

    I noticed 2 things.. one was what you mentioned. It only works for announcements. Can it work for ALL posts/pages? Or do I have to specify each one in your code?

    Second, the ones it doesn’t show up in (basically all of them but announcements) all the quick links are gone and don’t show anymore. It won’t matter ultimately if I can get this working on all post types but just wanted to run that by you.

    Thank you so much!! Brilliant.

  • Vaughan
    • Support/SLS MockingJay

    Hi Mike,

    that’s how it was originally when wedid it. lol i made the mod for announcements only when i saw your screenshot. I should have asked really, but no loss. it’s only 2 lines to remove to make it work for all post types.

    I’m attaching the version that should work for all post types.

    the reasonit stopped emworking on the other post typesis a simple mistake by me :slight_smile:

    I added the closing brace } after the return $actions line instead of just before it. lol

    Hope this helps

  • Vaughan
    • Support/SLS MockingJay

    Hi mike,

    Yes,

    Open up and edit the following file:

    /wp-content/plugins/wpmudev-custom-post-status-switch/wpmudev-custom-post-status-switch.php

    Find the following;

    add_filter( 'post_row_actions', 'wpmudev_custom_links_in_row_actions', 10, 2 );

    Then add the following line above it;

    add_filter( 'page_row_actions', 'wpmudev_custom_links_in_row_actions', 10, 2 );

    Hope this helps

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.