Skip to content

Instantly share code, notes, and snippets.

@simonwheatley
Created June 9, 2015 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonwheatley/e98c99c22eda112fb3ba to your computer and use it in GitHub Desktop.
Save simonwheatley/e98c99c22eda112fb3ba to your computer and use it in GitHub Desktop.
Demo of a plugin adding two fields in a meta box, one of which is synced and one translated
<?php
/*
Plugin Name: Babble Demo
Description: Demo to show translating (or not) post meta
Version: 1.0
Author: Automattic
Author URI: https://automattic.com/
License: GPL v2 or later
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* START ADDING A META BOX
* ============================
*/
/**
* Adds a box to the main column on the Post edit screen.
*/
function myplugin_add_meta_box() {
$screens = array( 'post' );
foreach ( $screens as $screen ) {
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_meta_box_callback',
$screen
);
}
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );
/**
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function myplugin_meta_box_callback( $post ) {
// Add a nonce field so we can check for it later.
wp_nonce_field( 'myplugin_meta_box', 'myplugin_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
echo '<label for="myplugin_new_field">';
_e( 'Description for this TRANSLATED field', 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />';
echo "<br />";
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value_2 = get_post_meta( $post->ID, '_my_meta_value_key_2', true );
echo '<label for="myplugin_new_field_2">';
_e( 'Description for this UNTRANSLATED field', 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="myplugin_new_field_2" name="myplugin_new_field_2" value="' . esc_attr( $value_2 ) . '" size="25" />';
}
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
function myplugin_save_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set, this gives us a first check
// that our form has been submitted.
if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
return;
}
$nonce = $_POST['myplugin_meta_box_nonce'];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce, 'myplugin_meta_box' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
$post = get_post( $post_id );
// Check this is a post. It's more important than ever, when using
// Babble, to check the actual $post being saved matches your
// expectation, as other post objects may be saved, triggering
// save_post actions of their own.
if ( 'post' != $post->post_type ) {
return;
}
// Check the user can edit this post
if ( ! current_user_can( 'edit_post', $post->ID ) ) {
return;
}
/* OK, it's safe for us to save the data now. */
// Make sure that the fields are set.
if ( ! isset( $_POST['myplugin_new_field'] ) && ! isset( $_POST['myplugin_new_field_2'] ) ) {
return;
}
// Sanitize user input.
$safe_my_data = sanitize_text_field( $_POST['myplugin_new_field'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_my_meta_value_key', $safe_my_data );
// Sanitize the other user input.
$safe_my_data_2 = sanitize_text_field( $_POST['myplugin_new_field_2'] );
// Update the other meta field in the database.
update_post_meta( $post_id, '_my_meta_value_key_2', $safe_my_data_2 );
}
add_action( 'save_post', 'myplugin_save_meta_box_data' );
/*
* END ADDING A META BOX
* ==========================
*/
/*
* START SPECIFYING WHICH FIELDS TO TRANSLATE
* ===============================================
*/
/**
* Hooks the bbl_translated_meta_fields Babble filter to add our fields to
* the list of fields which have translation configurations and so will show
* up in the translation UI.
*
* @param array $fields An array of instances of the Babble_Meta_Field_* classes
* @param WP_Post $post The WP_Post object which is to be translated
*
* @return array An array of instances of the Babble_Meta_Field_* classes
*/
function myplugin_bbl_translated_meta_fields( array $fields, WP_Post $post ) {
// There are various translation UI field types available:
// * Babble_Meta_Field_Text – a single text field
// * Babble_Meta_Field_Textarea – a text area
// * Babble_Meta_Field_Editor – a rich text editor
// The index, in this case `_my_meta_value_key`, should be the name of
// the `meta_key` for the post meta.
$fields[ '_my_meta_value_key' ] = new Babble_Meta_Field_Text( $post, '_my_meta_value_key', 'My Translated Field (1)' );
return $fields;
}
add_filter( 'bbl_translated_meta_fields', 'myplugin_bbl_translated_meta_fields', 10, 2 );
/**
* Hooks the bbl_sync_meta_key Babble filter to specify when a meta_key
* should not be translated. If a key is NOT to be translated, normally
* you will want Babble to sync the same value to all translations, e.g.
* for a meta_key which specifies the same custom header colour for a post
* whichever language it is in. If you want a meta_key to be translated, e.g.
* for a meta_key which specifies the text for a subheading, then you will
* want to specify a translation configuration AND stop the key from being
* synced by returning false in this filter when the $meta_key value is
* the name of your meta_key.
*
* @param bool $sync True if the meta_key is NOT to be translated and SHOULD be synced
* @param string $meta_key The name of the post meta meta_key
*
* @return bool True if the meta_key is NOT to be translated and SHOULD be synced
*/
function myplugin_bbl_sync_meta_key( $sync, $meta_key ) {
$sync_not = array( '_my_meta_value_key' );
if ( in_array( $meta_key, $sync_not ) ) {
return false;
}
return $sync;
}
add_filter( 'bbl_sync_meta_key', 'myplugin_bbl_sync_meta_key', 10, 2 );
/*
* END SPECIFYING WHICH FIELDS TO TRANSLATE
* ===============================================
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment