Search code examples
phpwordpresswordpress-admin

Wordpress admin: How to add a custom command to the admin post screen


In Wordpress admin, looking at a custom post type list screen enter image description here

Task: I need to copy the text of the column "Shortcode" (the text is already shown in the list).

Problem: I cannot access the text as the activated plugin "Post Types Order" inhibits overlaying the text by showing me a mouse pointer as part of some means to move the post to some other position (a feature I need)

My idea: When hovering the mouse over a post a number of command-buttons ("Edit", "QuickEdit" and "Trash") show up on the left hand side as can be seen here enter image description here

An additional command-button "Copy Shortcode" to this set of commands could do the job for me.

Question: How do I add a button to this list right next to "Trash"? (copying to the clipboard using Javascript should be ok)


Solution

  • I came up with a more comfortable solution that now fills the column "Shortcode" with hyperlinks of the shortcode text. Thus a simple click on the desired shortcode copies the shortcode-text to the clipboard

    enter image description here

    Here is the code:

    // add column 'Shortcode' in admin 'Layouts' list page
        // filter & action
        add_filter( 'manage_posts_columns', 'jg_add_id_column', 5 );
        add_action( 'manage_posts_custom_column', 'jg_id_column_content', 5, 2 );
    
        //add_filter( 'manage_et_pb_section_columns', 'jg_add_id_column', 5 );
        //add_action( 'manage_et_pb_section_custom_column', 'jg_id_column_content', 5, 2 );
    
        // display column title
        function jg_add_id_column( $columns ) {
          if( get_post_type( $post_id ) == 'et_pb_layout') {
             $columns['jg_id'] = 'Shortcode';
          }
          return $columns;
        }
    
        // display column value = shortcode-string
        function jg_id_column_content( $column, $id ) {
          if( get_post_type( $post_id ) == 'et_pb_layout') {
            if( 'jg_id' == $column ) {
              echo '<a id="myButton',$id, '" name="myButton',$id, '" class="myButtonClass">[showmodule id="', $id, '"]</a>';
            }
          }
        }
    

    The Javascript for copying the text to the clipboard was created according to the stackoverflow.com post 'Click button copy to clipboard using jQuery'. Thanks for the helpful comments.