Metabox Vendor ID 1
class sqpp_title_replacement {
public function __construct() {
if ( is_admin() ) {
add_action( 'load-post.php', array( $this, 'init_metabox' ) );
add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
}
}
public function init_metabox() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}
public function add_metabox() {
add_meta_box(
'sqpptitlereplacement',
__( 'Title Replacement', 'sqppcptmeta' ),
array( $this, 'render_sqpptitlereplacement' ),
'page',
'normal',
'low'
);
}
public function render_sqpptitlereplacement( $post ) {
// Add nonce for security and authentication.
wp_nonce_field( 'nonce_action', 'nonce' );
// Retrieve an existing value from the database.
$sqpptitlereplacetext = get_post_meta( $post->ID, 'sqpptitlereplacetext', true );
$sqpptitlereplacement_yn = get_post_meta( $post->ID, 'sqpptitlereplacement_yn', true );
$sqpptitlereplacetxtarea = get_post_meta( $post->ID, 'sqpptitlereplacetxtarea', true );
// Set default values.
if( empty( $sqpptitlereplacetext ) ) $sqpptitlereplacetext = '';
if( empty( $sqpptitlereplacement_yn ) ) $sqpptitlereplacement_yn = array();
if( empty( $sqpptitlereplacetxtarea ) ) $sqpptitlereplacetxtarea = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="sqpptitlereplacetext" class="sqpptitlereplacetext_label">' . __( 'Title Replacement', 'sqppcptmeta' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="sqpptitlereplacetext" name="sqpptitlereplacetext" class="sqpptitlereplacetext_field" placeholder="' . esc_attr__( '', 'sqppcptmeta' ) . '" value="' . esc_attr( $sqpptitlereplacetext ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="sqpptitlereplacement_yn" class="sqpptitlereplacement_yn_label">' . __( 'Replace Title', 'sqppcptmeta' ) . '</label></th>';
echo ' <td>';
echo ' <label><input type="checkbox" name="sqpptitlereplacement_yn[]" class="sqpptitlereplacement_yn_field" value="' . esc_attr( '' ) . '" ' . ( in_array( '', $sqpptitlereplacement_yn )? 'checked="checked"' : '' ) . '> ' . __( '', 'sqppcptmeta' ) . '</label><br>';
echo ' <p class="description">' . __( 'Do you want to replace the title? If so, then add a text in the field below.', 'sqppcptmeta' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="sqpptitlereplacetxtarea" class="sqpptitlereplacetxtarea_label">' . __( 'Textarea', 'sqppcptmeta' ) . '</label></th>';
echo ' <td>';
echo ' <textarea id="sqpptitlereplacetxtarea" name="sqpptitlereplacetxtarea" class="sqpptitlereplacetxtarea_field" placeholder="' . esc_attr__( '', 'sqppcptmeta' ) . '">' . $sqpptitlereplacetxtarea . '</textarea>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Add nonce for security and authentication.
$nonce_name = isset( $_POST['nonce'] ) ? $_POST['nonce'] : '';
$nonce_action = 'nonce_action';
// Check if a nonce is set.
if ( ! isset( $nonce_name ) )
return;
// Check if a nonce is valid.
if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) )
return;
// Check if the user has permissions to save data.
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
// Check if it's not an autosave.
if ( wp_is_post_autosave( $post_id ) )
return;
// Check if it's not a revision.
if ( wp_is_post_revision( $post_id ) )
return;
// Sanitize user input.
$new_sqpptitlereplacetext = isset( $_POST[ 'sqpptitlereplacetext' ] ) ? sanitize_text_field( $_POST[ 'sqpptitlereplacetext' ] ) : '';
$new_sqpptitlereplacement_yn = isset( $_POST[ 'sqpptitlereplacement_yn' ] ) ? array_intersect( (array) $_POST[ 'sqpptitlereplacement_yn' ], array( '' ) ) : array();
$new_sqpptitlereplacetxtarea = isset( $_POST[ 'sqpptitlereplacetxtarea' ] ) ? sanitize_text_field( $_POST[ 'sqpptitlereplacetxtarea' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'sqpptitlereplacetext', $new_sqpptitlereplacetext );
update_post_meta( $post_id, 'sqpptitlereplacement_yn', $new_sqpptitlereplacement_yn );
update_post_meta( $post_id, 'sqpptitlereplacetxtarea', $new_sqpptitlereplacetxtarea );
}
}
new sqpp_title_replacement;