PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 5, 2022

[FIXED] WordPress not honoring translations of _x() label functions of custom post type

 February 05, 2022     custom-post-type, gettext, poedit, wordpress     No comments   

Issue

In a WordPress theme I’m developing, I registered a custom post type called 'Project' as follows.

function add_custom_post_type() {

register_post_type( 'project', 
    array(
        'label'         => __( 'Projects', 'maria' ),
        'labels'        => array(
            'name'               => _x( 'Projects', 'post type general name', 'maria' ),
            'singular_name'      => _x( 'Project', 'post type singular name', 'maria' ),
            'menu_name'          => _x( 'Projects', 'admin menu', 'maria' ),
            'name_admin_bar'     => _x( 'Project', 'add new on admin bar', 'maria' ),
            'add_new'            => _x( 'Add New', 'project', 'maria' ),
            'add_new_item'       => __( 'Add New Project', 'maria' ),
            'new_item'           => __( 'New Project', 'maria' ),
            'edit_item'          => __( 'Edit Project', 'maria' ),
            'view_item'          => __( 'View Project', 'maria' ),
            'all_items'          => __( 'All Projects', 'maria' ),
            'search_items'       => __( 'Search Projects', 'maria' ),
            'parent_item_colon'  => __( 'Parent Projects:', 'maria' ),
            'not_found'          => __( 'No Projects found.', 'maria' ),
            'not_found_in_trash' => __( 'No Projects found in Trash.', 'maria' )
            ),
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor', 'thumbnail' ),
        'rewrite'       => array( 'slug' => __( 'project' ))
        )
    );
}
add_action( 'init', 'add_custom_post_type' );

Using the app Poedit, I am now trying to translate the labels of my custom post type to German. I am importing the functions/keywords __,_e and _x. The strings show up as expected.

https://abload.de/img/screenshot2019-09-29av4jhv.png (Screenshot)

However, upon translating my .po file and compiling the .mo file for WordPress localization, WordPress only seems to honor the translations I input for the __ and _e functions while ignoring the _x functions.

https://abload.de/img/screenshot2019-09-29ah7jxx.png (Screenshot)

In my dashboard, the Projects and Add New buttons still show up in English despite having been translated to Projekte and Erstellen respectively.

What am I doing wrong? Thanks very much in advance.


Solution

Okay, while I couldn’t solve the problem of WordPress not honoring the translations for _x functions, I got the labels to display correctly by using the following workaround.

In my register_post_type function, I changed all _x functions to __ functions (removing the context argument in the process). Final working code below:

function add_custom_post_type() {

register_post_type( 'project', 
    array(
        'label'         => __( 'Projects', 'maria' ),
        'labels'        => array(
            'name'               => __( 'Projects', 'maria' ),
            'singular_name'      => __( 'Project', 'maria' ),
            'menu_name'          => __( 'Projects', 'maria' ),
            'name_admin_bar'     => __( 'Project', 'maria' ),
            'add_new'            => __( 'Add New', 'maria' ),
            'add_new_item'       => __( 'Add New Project', 'maria' ),
            'new_item'           => __( 'New Project', 'maria' ),
            'edit_item'          => __( 'Edit Project', 'maria' ),
            'view_item'          => __( 'View Project', 'maria' ),
            'all_items'          => __( 'All Projects', 'maria' ),
            'search_items'       => __( 'Search Projects', 'maria' ),
            'parent_item_colon'  => __( 'Parent Projects:', 'maria' ),
            'not_found'          => __( 'No Projects found.', 'maria' ),
            'not_found_in_trash' => __( 'No Projects found in Trash.', 'maria' )
            ),
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor', 'thumbnail' ),
        'rewrite'       => array( 'slug' => __( 'project' ))
        )
    );
}
add_action( 'init', 'add_custom_post_type' );


Answered By - wandervogel
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing