job.answiz.com
  • 4
Votes
name

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 Employeebutton)

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_advancededit_form_after_editoredit_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.

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).

Kept running into a similar issue, the arrangement I utilized was as per the following: 

I guided into 'admin_notices' 

revamped my own header 

utilized CSS to shroud the current header like so: 

work rewrite_cpt_header(){ 

$screen = get_current_screen(); 

in the event that( $screen->id !='edit-area' ){ 

return; 

} else { 

?> 

<div class="wrap"> 

<h1 class="wp-heading-inline appear" style="display:inline-block;">Locations</h1> 

<a href="<?php reverberation admin_url('post-new.php?post_type=location'); ?>" class="page-title-activity show">Add New Location</a> 

<a href="<?php reverberation admin_url('edit-tags.php?taxonomy=location_types&post_type=location'); ?>" class="page-title-activity 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');

  • 1
Reply Report

Like this:

function custom_js_to_head() {
    ?>
    <script>
    jQuery(function(){
        jQuery("body.post-type-YOUR-CUSTOM-POST-TYPE .wrap h1").append('<a href="index.php?param=your-action" class="page-title-action">Import from ...</a>');
    });
    </script>
    <?php
}
add_action('admin_head', 'custom_js_to_head');

That code (pasted to functions.php) will dynamically add new link tag after "Add new" link.

  • 1
Reply Report

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 
}
  • 4
Reply Report