1

I have several different posts type, each with a media upload. What I want to do is to rename the "insert into post" button so it correlates to the individual custom-posts. For example - custom post type - "award" and then the media upload box would have the following button, "insert into award".

Any ideas?

2 Answers 2

2

I think, this is not so easy possible, on the media-page in Admin-area is it not possible to check the post_type of your CPT. Normaly you can change strings with the follow small source, an example.

if ( is_admin() )
    add_filter( 'gettext', array( 'fb_string_translate', 'gettext_filter' ), 10, 1 );
class fb_string_translate {

    static function gettext_filter( $str ) {

        $from_to = array();

        $post_type = get_post_type();
        if ( 'my_post_type' === $post_type )
            $from_to = array( 'Insert into Post' => 'Insert into Test' );

        return strtr($str, $from_to);
    }

}

But it is important, that you check, on wich page is the author in admin and on the iframe of the media page it is not possible to read the post_type; maybe you find this var and then it is possible with the example source.

0

[update]
I took a silly approach at first and am completely rewriting my answer due to this Stephen Harris' post. Result: it is much more compact now.

I tried to adapt @bueltge's filter into my code, but didn't succeed.

My solution uses jQuery to manipulate the strings.
In the From Computer tab I'm adding a function to run every half second to be able to change the button text when it appears (after uploading).

This example deals with 2 CPTs: "movie" and "woo_estate".

/*
 * Change the text "Use this Image"/"Insert into Post" on the upload window to "Insert into CPT"
*/
function wpse_26585_script_enqueuer(){
    global $current_screen;
    $post_id = !empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;
    $post_type = get_post_type($post_id);

    if( 'media-upload' == $current_screen->id && ('movie' == $post_type || 'woo_estate' == $post_type) ) {

        // VAR DECLARATION
        $select = "";
        if('movie' == $post_type)       $select = "Insert into Movie";
        if('woo_estate' == $post_type)  $select = "Insert into Property";

        // FIND CURRENT TAB
        $tab     = isset($_GET['tab']) ? $_GET['tab'] : "type";

        // CHANGE NAMES IN UPLOAD MEDIA TAB and UPDATE "SAVE ALL CHANGES" LINK TO INCLUDE OUR PARAMETER IN THE QUERY
        $jquery = ('type' == $tab) ? 'var refreshUpload = setInterval(function(){$("#media-items").each(setButtonNames);},500);' : '$("#media-items").each(setButtonNames);';

        //CHANGE BUTTON NAMES
        echo <<<HTML
        <script type="text/javascript">
        function setButtonNames() {
            jQuery(this).find('.savesend .button').val('{$select}');
        }
        jQuery(document).ready(function($){            
            {$jquery}
        });
        </script>
HTML;
    }
}
add_action('admin_head', 'wpse_26585_script_enqueuer');

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.