4

I want to replace the dashes before the page title in the dashboard page list. For each hierarchy below the first, a dash is prepended (as seen in the screenshot below):

enter image description here

It seems that the filter the_title does not affect these dashes:

add_filter( 'the_title', 'change_my_title' );
function change_my_title( $title ) {
    return str_replace( '–', $title );
    // nor preg_replace or – or — work
}

So my question is: How can I replace these dashes with something specific? Do I really have to implement a custom list table or meddle around with jQuery?

  • Those dashes is indicating the hierarchy level of the post item and it's not inside the the_title filter. Using the_title filter you can only change the post title. – Govind Kumar Dec 6 '16 at 9:22
3

You can't change dashes using any filter because there is no filter available to change it.

but still you can change this using jQuery put this code inside functions.php

add_action('admin_head',function(){


global $pagenow;

// check current page.
if( $pagenow == 'edit.php' ){ ?>

    <script>

        jQuery(function($){

            var post_title = $('.wp-list-table').find('a.row-title');
            $.each(post_title,function(index,em){
                var text = $(em).html();
                // Replace all dashes to * 
                $(em).html(text.replace(/—/g ,'*'));
            });
        });

    </script>
    <?php
    }
});

See https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/class-wp-posts-list-table.php#L918-L919

3

This is hard-coded and cannot be changed without changing the entire list table class:

    $pad = str_repeat( '&#8212; ', $this->current_level );
    echo "<strong>";

    $format = get_post_format( $post->ID );
    if ( $format ) {
        $label = get_post_format_string( $format );

        $format_class = 'post-state-format post-format-icon post-format-' . $format;

        $format_args = array(
            'post_format' => $format,
            'post_type' => $post->post_type
        );

        echo $this->get_edit_link( $format_args, $label . ':', $format_class );
    }

    $can_edit_post = current_user_can( 'edit_post', $post->ID );
    $title = _draft_or_post_title();

    if ( $can_edit_post && $post->post_status != 'trash' ) {
        printf(
            '<a class="row-title" href="%s" aria-label="%s">%s%s</a>',
            get_edit_post_link( $post->ID ),
            /* translators: %s: post title */
            esc_attr( sprintf( __( '&#8220;%s&#8221; (Edit)' ), $title ) ),
            $pad,
            $title
        );
    } else {
        echo $pad . $title;
    }

And even changing that table is very difficult, because the class instance is set to a global variable with the function _get_list_table(), which doesn't even offer a filter.

Welcome to the wonderful world of procedural code.

I guess you have to use JavaScript for that.

3

As @Toscho says, the title column is hard coded, so you cannot change that. What you can do, however, is remove the column and redefine it as a custom column:

add_filter( 'manage_pages_columns', 'wpse248405_columns', 25, 1 );
function wpse248405_columns ($cols) {
   // remove title column
   unset( $cols['title'] );
   // add custom column in second place
   $cols = array('cb' => $cols['cb']) + array('title' => __( 'Title', 'textdomain' )) + $cols;
   // return columns
   return $cols;
   }

Now you have to make the custom column behave like the original:

add_action( 'manage_pages_custom_column', 'wpse248405_custom_column', 10, 2 );
function wpse248405_custom_column( $col, $post_id ) {
    if ($col == 'title') {
        $post               = get_post( $post_id );
        $title              = _draft_or_post_title();
        $can_edit_post      = current_user_can( 'edit_post', $post->ID );
        // set up row actions
        $actions = array();
        if ( $can_edit_post && 'trash' != $post->post_status ) {
            $actions['title'] = '<strong><a href="' . get_edit_post_link( $post->ID, true ) . '" aria-label="' . $title . esc_attr( __( 'Edit this item' ) ) . '">' . $title . '</a></strong>';
            // invoke row actions
            $table = new WP_Posts_List_Table;
            echo $table->row_actions( $actions, true );
            }
        }
     }

Beware that if you mimick core behaviour in your own functions you become vulnerable to future core releases.

-1

The dashes are because you have them listed under parent pages. Lower right side of the page edit screen you can remove the parents if you want or need to.

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.