Generating pot file using WP CLI
- from local environment, open shell
- go to your plugin directory and type this
- type
$ wp i18n make-pot . languages/my-plugin.pot
Generating machine objects file from po file
$ wp i18n make-mo movie-library-hi_IN.po languages
Different functions used for i18n
__()- put the string inside __() to make it translatable
- eg :
__('Add new Movie','movie-library');
_e()- if the string should echo to the browser then use
__('Add new Movie','movie-library');_n()- give both singular and plular form of the string
_n( 'We deleted %d spam message.', // singular 'We deleted %d spam messages.', //plural $count, 'my-text-domain' )_x()- Sometimes same word may mean diffrent according to in which context it is used for that this is used.
_x( 'Attachment', 'A files attached to mail', 'my-text-domain' );_ex()- Use when we want to combine both
_e()and_x()
- Use when we want to combine both
array_diff()
How can we set the info selected by user and remove the user that is unselected in metabox?
We are only getting the selected ids and not what is unselected, for that we can get previously selected terms, compare them with currently selected and remove the ones that are not present in current.
For this, I used PHP array_diff() function,
Compares
arrayagainst one or more other arrays and returns the values inarraythat are not present in any of the other arrays.
First we get the selected ids and create terms
$all_people = array_merge( $directors, $producers, $writers, $actors );
$terms = [];
// Create array of terms to be added now.
foreach ( $all_people as $person_id ) {
$person = get_post( $person_id );
$term = $person->post_name . '-' . $person_id;
$term_exists = term_exists( $term, '_rt_movie-person' );
// Create the term if it does not exists.
if ( ! $term_exists ) {
wp_insert_term( $term, '_rt-movie-person', [ 'slug' => $term ] );
}
$terms[] = $term;
} // end foreach
Then we get the terms selected before
// Get previous terms.
$old_terms = wp_get_post_terms( $post_id, '_rt-movie-person', [ 'fields' => 'slug' ] );
Compare the both using array_diff() and get terms which are not in the current but were there in previous
// Get unselected Terms.
$unselected_terms = array_diff( $old_terms, $terms );
Finally remove the relationship between terms and out post
wp_remove_object_terms( $post_id, $unselected_terms, '_rt-movie-person' );
This way, the term relationship will get deleted when unselected in the metabox
WPDB – standard way to interact with WordPress database
wpdb is a class provided by WordPress that has functions to handle database queries without having to write raw SQL.
- It is used so we can query the wordpress database without having to write raw SQL queries.
- Create an abstraction layer over the database.
- Common methods
insert(): insert data into a tableprepare(): prepares sql query to avoid sql injectionupdate(): to update data in tabledelete(): to delete data
Leave a Reply