Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Button/Link Immediately After Title to Custom Post Type Edit Screen

I'm customizing the backend of a WordPress installation for a client, and I've added several custom post types. They are displayed on the front-end via a custom loop inside one page, so users shouldn't access the individual custom posts.

To help ensure they aren't accidentally accessed, I've removed the Permalink selection box, which includes the View Post button. I'd like to add a View Post link at the top of the page, beside Add New Employee. (If not possible, I would settle for intercepting and replacing the Add New Employee button)

I'm looking for a WordPress hook to place the button there, but I haven't found anything yet. The closest I found was a post on the WP Core blog (citing edit_form_advanced, edit_form_after_editor, edit_form_after_title), but none of the ones listed will work for this.

The button I'd like to add will be this PHP:

<a class="add-new-h2" href="<?php echo $displaypage.get_the_title($post_ID); ?>">View <?php echo $posttype; ?></a>

With $displaypage being something along the lines of '/about-us/our-people/#'

Really though, all I need help on is finding a way to hook it in. I know what to add once I find out how to add it.

Test

Edit:

I know that I could insert the button using javascript, without bothering with hooks at all. I'd like to avoid that if possible though, for edge cases of people who disable JS, and people who have very slow connections (jQuery won't activate until the page is fully loaded, so it'll flash in late in that situation).

like image 342
Andy Mercer Avatar asked Mar 07 '14 21:03

Andy Mercer


2 Answers

I only found one hook not cited in WP Core's post and seems the only one nearest to the desired position:

enter image description here

The problem is that Add New (post type) is wrapped inside an <h2> tag (line 365 of the same file) and the hook happens after it.

add_action( 'edit_form_top', 'top_form_edit' );

function top_form_edit( $post ) {
    if( 'portfolio' == $post->post_type )
        echo "<a href='#' id='my-custom-header-link'>$post->post_type</a>";
}

The only option left is to manipulate the DOM with jQuery:

add_action( 'admin_head-post.php', 'manipulate_dom' );
add_action( 'admin_head-post-new.php', 'manipulate_dom' );

function manipulate_dom()
{
    // Not our post type, exit earlier
    // Adjust post type
    if( 'portfolio' != get_current_screen()->post_type )
        return;
    ?>
    <script type="text/javascript">
        jQuery(document).ready( function($) 
        {
            // your thing
            // $('#my-custom-header-link').moveAround();        
        });     
    </script>
    <?php 
}
like image 191
brasofilo Avatar answered Nov 13 '22 12:11

brasofilo


Ran into the same problem, the solution I used was as follows:

  • I hooked into 'admin_notices'

  • rewrote my own header

  • used CSS to hide the existing header like so:

    function rewrite_cpt_header(){
           $screen = get_current_screen();
         if( $screen->id !='edit-location' ){
             return;
         } else {
             ?>
             <div class="wrap">
             <h1 class="wp-heading-inline show" style="display:inline-block;">Locations</h1>
             <a href="<?php echo admin_url('post-new.php?post_type=location'); ?>" class="page-title-action show">Add New Location</a>
             <a href="<?php echo admin_url('edit-tags.php?taxonomy=location_types&post_type=location'); ?>" class="page-title-action show">Edit Location Types</a>
             </div>
    
             <style scoped>
             .wp-heading-inline:not(.show),
             .page-title-action:not(.show) { display:none !important;}
             </style>
             <?php
         }
     }
     add_action('admin_notices','rewrite_cpt_header');
    
like image 36
Christian Žagarskas Avatar answered Nov 13 '22 12:11

Christian Žagarskas