0

As a priority i need to just display some custom html inside the Title(h1.wp-heading-inline) in the Post Edit Screen (Top Left Corner). While trying various methods i found that all html is filtered only text is displayed.

If there is no way to prevent the filtering of html of H1 title in admin page then i would like to remove it for Custom Post Type's posts.

Here is what i tried:

add_action( 'edit_form_top', [$this, 'customPostTitle' ] );
function customPostTitle($title) {
        global $post;
        if ($post->post_type == $this::POST_TYPE['id']) {
            $custom_post_title = $post->post_title;
            $custom_title_fin = __( 'Custom post title: ', 'custom-plugin' ) . '<span class="custom-class">'. $custom_post_title . '</a>';
            $title = $custom_title_fin;
        }

        return $title;
    }

Is there any actions/filters i can do to achieve this ?

I don't want to use js/css - i am looking for a native/php way ?

1
  • FYI the edit_form_top hook is an action, not a filter, so return does nothing.
    – Caleb
    Jun 23, 2023 at 13:35

1 Answer 1

0

Changing "Add New Post" to a string with unescaped HTML tags cannot be accomplished using only PHP: WordPress core escapes any HTML in the CPT's label for that text.

Additionally (again, only using PHP), there is no way to remove that block of text: setting the add_new_item label to blank when registering the CPT results in "Add New" being displayed.

Your best option is to use CSS to hide the core text, and then add your own <h1>:

add_action( 'edit_form_top', static function () {
    if ( empty( $_GET['post'] ) ) {
        return;
    }

    $post_id = absint( $_GET['post'] );

    if ( 'wpse416980' !== get_post_type( $post_id ) ) {
        return;
    }

    echo '<style>.wrap h1.wp-heading-inline { display: none; }</style>';

    printf( 
        '<h1>%s<span class="custom-class">%s</span></h1>',
        __( 'Custom post title: ', 'custom-plugin' ),
        get_the_title( $post_id )
    );
} );
1
  • Thanks. Shame there is no hook for post admin title...
    – Ion T
    Jun 29, 2023 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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