I m creating a wp plugin which supports custom post type. In that i have created a custom meta box..when i enter the metabox values it will not save into the admin dashboard as well as database. Can any one help me out this.
Thanks..
//enter code here
function astestimonial_meta_box($post_id)
{
wp_nonce_field('astest_meta_box','astest_meta_box_nonce');
$clientname = get_post_meta($post->ID,'_clientname_value','true');
echo'<label for="client_name_val">';
_e('Name: ', 'astestimonial_textdomain');
echo'</label>';
echo'<input type="text" id="client_name_val" name="client_name_val" value="'.esc_attr($clientname).'" />';
}
//Save meta data code
function astest_save_client_info($post_id)
{
if(!isset($_POST['astest_meta_box_nonce']))
{
return;
}
if(!wp_verify_nonce($POST['astest_meta_box_nonce'],'astest_meta_box'))
{
return;
}
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
{
return;
}
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] )
{
if ( ! current_user_can( 'edit_post', $post_id ) )
{
return;
}
}
else
{
if ( ! current_user_can( 'edit_page', $post_id ) )
{
return;
}
}
if( !isset( $_POST['client_name_val']) && !isset($_POST['client_email_val']) && !isset($_POST['client_comp_val']) && !isset($_POST['client_desig_val']))
{
return;
}
$cli_name_data = sanitize_text_field( $_POST['client_name_val'] );
update_post_meta($post_id, '_clientname_value', $cli_name_data);
}
add_action( 'save_post', 'astest_save_client_info' );
Related
I've successfully been able to create one custom metabox for WordPress post types using the following code:
function bandcamp_metabox(){
$posttypes = Array(
'discos',
'Projetos'
);
add_meta_box('bandcamp_link', 'bandcamp link', 'bandcamp_link_meta_callback', $posttypes, 'side');
}
function bandcamp_link_meta_callback( $post ) {
wp_nonce_field('save_bandcamp_link', 'bancamp_meta_box_nonce');
$value = get_post_meta($post->ID,'_bandcamp_link_value_key', true);
echo '<label for="bancamp_link_field">';
echo '<input type="url" id="bancamp_link_field" name="bancamp_link_field" value="' . esc_attr ( $value ) . '" size="25" />';
}
function save_bandcamp_link ( $post_id ) {
if( ! isset( $_POST['bancamp_meta_box_nonce'] ) ){
return;
}
if ( ! wp_verify_nonce( $_POST['bancamp_meta_box_nonce'], 'save_bandcamp_link' ) ){
return;
}
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return;
}
if( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
if( ! isset( $_POST['bancamp_link_field'] ) ) {
return;
}
$bandcamp_link = sanitize_text_field( $_POST['bancamp_link_field'] );
update_post_meta($post_id, '_bandcamp_link_value_key', $bandcamp_link);
}
add_action('add_meta_boxes', 'bandcamp_metabox');
add_action('save_post', 'save_bandcamp_link');
I was hoping (still kind of a noob) that through a 'foreach' function I'd be able to re-use the code to create multiple metaboxes at once like this:
$varsocial = Array(
'bandcamp',
'soundcloud',
'facebook',
'instagram',);
foreach($varsocial as $sociallink) {
function social_metabox(){
$posttypes = Array(
'discos',
'Projetos');
add_meta_box(''. $sociallink .'_link', ''. $sociallink .' link', 'social_link_meta_callback', $posttypes, 'side');
}
function social_link_meta_callback( $post ) {
wp_nonce_field('save_social_link', 'social_meta_box_nonce');
$value = get_post_meta($post->ID,'_'. $sociallink .'_link_value_key', true);
echo '<label for="'. $sociallink .'_link_field">';
echo '<input type="url" id="'. $sociallink .'_link_field" name="'. $sociallink .'_link_field" value="' . esc_attr ( $value ) . '" size="25" />';
}
function save_social_link ( $post_id ) {
if( ! isset( $_POST['social_meta_box_nonce'] ) ){
return;
}
if ( ! wp_verify_nonce( $_POST['social_meta_box_nonce'], 'save_social_link' ) ){
return;
}
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return;
}
if( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
if( ! isset( $_POST[''. $sociallink .'_link_field'] ) ) {
return;
}
$social_link = sanitize_text_field( $_POST[''. $sociallink .'_link_field'] );
update_post_meta($post_id, '_'. $sociallink .'_link_value_key', $social_link);}
return add_action('add_meta_boxes', 'social_metabox');
return add_action('save_post', 'save_social_link');
}
But such is not working. What am I doing wrong?
I have written the following code, but it won't save the state. The checkbox always reverts to being checked:
<?php
/*META BOXES*/
function add_film_meta_boxes() {
add_meta_box('film_meta_data', 'Film info:', 'film_meta_box_callback', 'film', 'advanced', 'high');
}
function film_meta_box_callback( $post ) {
wp_nonce_field('save_film_meta_data', 'spanish_meta_box_nonce');
$spanish = get_post_meta( $post->ID, '_spanish_value_key', true);
echo '<label>Subtitles: </label>';
$spanish_subtitle_field = get_post_meta($post->ID, 'spanish_subtitle_field', true);
if($spanish_subtitle_field == "yes") {$spanish_subtitle_checked = 'checked="checked"';} else {$spanish_subtitle_checked = '';}
echo '<label><input type="checkbox" id="spanish_subtitle_field" name="spanish_subtitle_field" value="yes" '.$spanish_subtitle_checked.' /></label><label for="spanish_subtitle_field" style="font-weight:normal !important;">spanish </label>';
}
function save_film_meta_data ($post_id) {
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return;
if ( ! current_user_can( 'edit_post', $post_id ))
return;
if( ! isset($_POST['spanish_meta_box_nonce']) )
return;
if ( ! wp_verify_nonce($_POST['spanish_meta_box_nonce'], 'save_film_meta_data') )
return;
if ( ! isset( $_POST['spanish_subtitle_field']))
return;
$spanish = isset($_POST['spanish_subtitle_field']) ? 'on' : 'off';
update_post_meta($post_id, '_spanish_value_key', $spanish);
}
?>
The code is included in my functions.php file.
This line is wrong, this make your metabox have a value 'on' or 'off'.
$spanish = isset($_POST['spanish_subtitle_field']) ? 'on' : 'off';
And when you check if is checked, you looking for a 'yes' value.
if($spanish_subtitle_field == "yes") {$spanish_subtitle_checked = 'checked="checked"';} else {$spanish_subtitle_checked = '';}
So try this new function and check if works:
function save_film_meta_data($post_id){
if(! isset($_POST['spanish_meta_box_nonce'])){
return;
}
if(!wp_verify_nonce($_POST['spanish_meta_box_nonce'],'save_film_meta_data')){
return;
}
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return;
}
if(! current_user_can('edit_post',$post_id)){
return;
}
if( ! isset($_POST['spanish_subtitle_field'])){
return;
}
$my_data = isset($_POST['spanish_subtitle_field']) ? $_POST['spanish_subtitle_field'] : 'no';
update_post_meta($post_id,'_spanish_value_key',$my_data);
}
Been banging my head against the wall trying to figure out the solution to my current issue. Simply put, I've created a 'page-home.php' template page, can get a meta box to call on the page template, but trying to update the page with data in the meta box makes the data disappear.
Code below:
function page_add_meta_boxes( $post ) {
global $post;
if(!empty($post))
// get the page template post meta
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
// if the current page uses our specific template, then output our custom metabox
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
// looks for page-home.php file to add our meta box
if($pageTemplate == 'page-home.php' )
{
add_meta_box(
'page-custom-metabox', // $id
'Special Post Meta', // $title
'page_template_metabox', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
}
}
}
add_action( 'add_meta_boxes_page', 'page_add_meta_boxes' );
function page_template_metabox() {
wp_nonce_field( basename( __FILE__ ), 'page_meta_box_nonce' );
$some_string = get_post_meta( $post->ID, '_some_string', true );
?>
<input type="text" name="some-string" value="<?php echo $some_string; ?>" />
<?php
}
function page_save_custom_post_meta() {
if ( !isset( $_POST['page_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['page_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// check the user's permissions.
if ( ! current_user_can( 'edit_page', $post_id ) ){
return;
}
// save our string
if ( isset( $_REQUEST['some-string'] ) ) {
update_post_meta( $post_id, '_some_string', sanitize_text_field( $_POST['some-string'] ) );
}
}
add_action( 'publish_page', 'page_save_custom_post_meta' );
add_action( 'draft_page', 'page_save_custom_post_meta' );
add_action( 'future_page', 'page_save_custom_post_meta' );
Any suggestions are appreciated.
Howdy anyone who runs into this in the future. I was able to get it to work with the code below, someone might be able to follow up with a better answer as to why it works. My thinking is that updating my code to 'save_post_page' instead of just 'save_post' makes the difference. (Note I only changed some info due to trying to test it in my theme):
// Add meta box
function frontpage_meta_boxes( $post ){
global $post;
if(!empty($post))
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'page-home.php' )
{
add_meta_box( 'frontpage_meta_box', __( 'Features' ), 'frontpage_meta_box', 'page', 'advanced', 'high' );
}
}
}
add_action( 'add_meta_boxes_page', 'frontpage_meta_boxes' );
// builds our meta box
function frontpage_meta_box( $post ){
// make sure the form request comes from WordPress
wp_nonce_field( basename( __FILE__ ), 'frontpage_meta_box_nonce' );
// retrieve the _manuf_url current value
$manufacturer_url = get_post_meta( $post->ID, '_manuf_url', true );
?>
<h3><?php _e( 'Manufacturer URL' ); ?></h3>
<p>
<input type="text" name="manufacturer-url" value="<?php echo $manufacturer_url; ?>" />
</p>
<?php
}
// saves our data
function frontpage_save_meta_box_data( $post_id ){
// verify meta box nonce
if ( !isset( $_POST['frontpage_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['frontpage_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
// manufacturer url string
if ( isset( $_REQUEST['manufacturer-url'] ) ) {
update_post_meta( $post_id, '_manuf_url', sanitize_text_field( $_POST['manufacturer-url'] ) );
}
// store custom fields values
}
add_action( 'save_post_page', 'frontpage_save_meta_box_data' );
Try with below code :
function page_save_custom_post_meta($postid) {
if ( !isset( $_POST['page_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['page_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// check the user's permissions.
if ( ! current_user_can( 'edit_page', $post_id ) ){
return;
}
// save our string
if ( isset( $_REQUEST['some-string'] ) ) {
update_post_meta( $post_id, '_some_string', sanitize_text_field( $_POST['some-string'] ) );
}
}
add_action('save_post ','page_save_custom_post_meta');
I am using the visual composer for the WordPress posts and pages actually for over all. But I want to make some custom meta boxes under the screen of the post editor. Actually already I have made the fields. But now I want to make those fields available in the visual composer. Actually I want to add those fields in the visual editor. How can I do that? Please help me with your valuable knowledge.
Here is my code of the meta boxes
<?php
function myplugin_add_meta_box() {
$screens = array( 'post', 'page' );
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' );
function myplugin_meta_box_callback( $post ) {
wp_nonce_field( 'myplugin_save_meta_box_data', 'myplugin_meta_box_nonce' );
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
echo '<label for="myplugin_new_field">';
_e( 'Description for this field', 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />';
}
function myplugin_save_meta_box_data( $post_id ) {
if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_save_meta_box_data' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['myplugin_new_field'] ) ) {
return;
}
// Sanitize user input.
$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', $my_data );
}
add_action( 'save_post', 'myplugin_save_meta_box_data' );
I see that you are echoing your fields as inputs. You need to use the wp_editor() function instead. It will take care of the wysiwyg (visual editor) field creation for you.
I am trying to add error message on custom field in custom post save action But my validation not working. Check my code below.
add_action( 'save_post', 'save_event_meta_data' );
function save_event_meta_data( $post_id ) {
$event_university = $_POST['event_university'];
if ( isset( $_POST['post_type'] ) && 'tribe_events' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
if($event_university=="") {
new WP_Error( 'Error', __( "Please select university" ) );
return;
}
$event_university_data = $event_university ;
update_post_meta( $post_id, 'event_university', $event_university_data );
}
please do the needful.
For displaying error message on other page, you need to save it in some variable. So try below code.
if($event_university=="") {
global $error;
$error = new WP_Error();
$error->add("Please select university");
return;
}
Then on the other page you can access that error message using below code:
global $error;
echo $error->get_error_message();