0

I've got a few special needs in terms of post formatting, but do not want (and cannot) break out of the standard Post Format paradigm per best practices.

I've come up with a mapping of WordPress core types to my company's own nomenclature (e.g. show "chat" as "discussion"). I'd like to (from a UI perspective only) rename these post formats to the internal names. I'd like to do this via filters if possible, and not via some JavaScript hack after load.

Is it possible to filter the display name for post formats for display in the Formats meta box?

2 Answers 2

2

Yes, you can. You can hook right into gettext. The example provided below changes Publish button:

add_filter( 'gettext', 'binda_change_publish_button', 10, 2 );

function binda_change_publish_button( $translation, $text ) {
    //check if this is pizza add or edit page in administration 
    global $pagenow, $typenow;
    if ( is_admin() && ( $pagenow == 'post-new.php' or $pagenow == 'post.php' ) && ( $typenow == 'your_post_type' or ( isset($_GET['post_type']) && $_GET['post_type'] == 'your_post_type') ) ) {
        if ( $text == 'Publish' )
            return 'Save My post type';     
    }
    return $translation;
}

EDIT: Tried to change Aside to "Postranní" (in Czech) and it works without problem when using another filter gettext_with_context. See example below:

add_filter( 'gettext_with_context', 'binda_change_standart_format_name', 10, 2 );

function binda_change_standard_fromat_name( $translation, $text ) {
    //check if this is pizza add or edit page in administration 
    global $pagenow, $typenow;
    if ( is_admin() && ( $pagenow == 'post-new.php' or $pagenow == 'post.php' ) && ( $typenow == 'post' or ( isset($_GET['post_type']) && $_GET['post_type'] == 'post') ) ) {
        if ( $text == 'Aside' )
            return 'Postranní';     
    }
    return $translation;
}
5
  • I just arrived there as well via the _x() method. Thank you!
    – editor
    Feb 11, 2013 at 21:18
  • 2
    _x will call the filter gettext_with_context. gettext only works for "Standard"...
    – brasofilo
    Feb 11, 2013 at 21:23
  • As I mentioned in modified answer, simple 'gettext' filer is working too... Feb 11, 2013 at 21:24
  • 1
    Check the other strings.
    – brasofilo
    Feb 11, 2013 at 21:25
  • U're right. I've updated my answer. Thank you for great point. Feb 11, 2013 at 21:31
2

[ Late answer: This is only valid/useful if you want to avoid using a get_text filter -- I always worry about performance using them ]

In 3.5.1, the Formats meta box is rendered on line 305 ( post_format_meta_box ) of "wp-admin/includes/meta-boxes.php".

This function calls "get_theme_support('post-formats')" located in "wp-includes/theme.php" and returns the original add_theme_support value inserted by the theme -- in this case, the array of post formats.

Theres no filter on the return value, just "return $_wp_theme_features[ $feature ];"

An alternative, if you want to avoid using Javascript/jQuery, it is possible to substitute your own Formats meta box by duplicating the code from the post_format_meta_box function with your own custom code:

function add_custom_formats_metabox( )
{
    remove_meta_box( 'formatdiv' , 'post' , 'side' );
    add_meta_box( 'my-formatdiv', 'Format' , 'render_custom_formats_metabox', 'post', 'side', 'core' );
}
function render_custom_formats_metabox( $post, $box ) {
    if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) ) :
        $post_formats = get_theme_support( 'post-formats' );

        if ( is_array( $post_formats[0] ) ) :
            $post_format = get_post_format( $post->ID );
            if ( !$post_format )
                $post_format = '0';
            if ( $post_format && !in_array( $post_format, $post_formats[0] ) )
                $post_formats[0][] = $post_format;
            ?>
            <div id="post-formats-select">
                <input type="radio" name="post_format" class="post-format" id="post-format-0" value="0" <?php checked( $post_format, '0' ); ?> /> <label for="post-format-0"><?php _e('Standard'); ?></label>
                <?php
                foreach ( $post_formats[0] as $format ) :
                // implement your own labeling logic here...
                $label = $format === 'chat' ? 'Discussion' : get_post_format_string( $format ) ;
                ?>
                <br /><input type="radio" name="post_format" class="post-format" id="post-format-<?php echo esc_attr( $format ); ?>" value="<?php echo esc_attr( $format ); ?>" <?php checked( $post_format, $format ); ?> /> <label for="post-format-<?php echo esc_attr( $format ); ?>"><?php echo esc_html( $label ); ?></label>
                <?php endforeach; ?><br />
            </div>
        <?php endif; endif;
}

add_action( 'add_meta_boxes' , 'add_custom_formats_metabox' );
2
  • 2
    Late one but nice one! The performance question is good point. But as long as this is on backend, I would be worry so much... Feb 11, 2013 at 22:06
  • 1
    Thanks. I hadn't considered wrapping the add_filter() in is_admin(). This sounds like one of those issues that should be in Trac: "get_post_format_strings" functions should have a filter -- considering it's returning a static array.
    – Jer
    Feb 11, 2013 at 22:13

Your Answer

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

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