How to use Metabox checkbox to add page ids in array? - php

What I'm trying to do is for a dual-language website. This particular language, Papiamento, isn't supported by Wordpress. Therefore, the client had to create two separate pages, in English and Pap. What I did was to code it like that to display English or Pap menu for each page.
Like this, in header.php:
<?php
if( is_page( array('salon-and-spa-pap', 'tocante-nos', 'testimonio', 'tuma-contacto-cu-nos', 'galeria', 'tratamentonan-di-masahe', 'tratamentonan-spa-di-curpa', 'servicionan-di-boda', 'tratamentonan-spa-di-cara', 'wowo-lip-nek', 'cuido-di-man', 'tratamento-di-huna', 'tratamento-di-huna-di-pia', 'cuido-di-pia', 'salon-p', 'spa-etiquette-pap', 'wax-p', 'reserva-un-tratamento')) ) {
wp_nav_menu(array( 'theme_location' => 'menu_top_pap' ) );
} else {
wp_nav_menu(array( 'theme_location' => 'secondary-menu' ) );
}
?>
However, the problem is that the client will have to keep going back to header.php to add another page slug every time she create a new page. Therefore, I created a Metabox plugin for that. I made a Metabox checkbox so that everytime a page is intended for Papiamento language, the client can just check the box and either the page id or slug will be added to the code above.
I found another question (https://wordpress.stackexchange.com/questions/71043/listing-pages-with-checkboxes-in-a-metabox-and-saving-them) that might be similar to what I was looking for but it didn't work for me.
Here's my metabox function based on this article (http://themefoundation.com/wordpress-meta-boxes-guide/).
<?php
function prfx_custom_meta() {
add_meta_box( 'prfx_meta', __( 'Papiamento Page Box', 'prfx-textdomain' ), 'prfx_meta_callback', 'page', 'normal', 'low' );
}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );
function prfx_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID );
$checkfield = maybe_unserialize( get_post_meta($post->ID, "checkfield", true) );
?>
<p>
<span class="prfx-row-title"><?php _e( 'Pap Checkbox', 'prfx-textdomain' )?></span>
<div class="prfx-row-content">
<label for="meta-checkbox">
<input type="checkbox" name="checkfield[]" id="page_<?php echo $page->ID; ?>" value="<?php echo $page->ID; ?>" <?php if ( in_array($page->ID, (array) $checkfield) ) { ?> checked <?php } ?>/> <label for="page_<?php echo $page->ID; ?>"><?php echo $page->post_title; ?>
<?php _e( 'Check if this page is Papiamento', 'prfx-textdomain' )?>
</label>
</div>
</p>
<?php
}
/**
* Saves the custom meta input
*/
function prfx_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and sanitizes/saves if needed
// Checks for input and saves
if( isset( $_POST[ 'checkfield' ] ) ) {
update_post_meta($post_id, "checkfield", $_POST['checkfield'] );
}
}
add_action( 'save_post', 'prfx_meta_save' );
and calling the page id in header.php like this:
<?php
if( in_array($page->ID, (array) $checkfield) ) {
wp_nav_menu(array( 'theme_location' => 'menu_top_pap' ) );
} else {
wp_nav_menu(array( 'theme_location' => 'secondary-menu' ) );
}
?>
But it didn't work. Please help!
***************** UPDATE *****************
I've been trying to edit the function prfx_meta_callback( $post ) but with no success. Here's the latest code I'm trying to modify...
function prfx_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$checkfield = maybe_unserialize( get_post_meta($post->ID, "checkfield", true) );
$page = get_pages();
$prfx_stored_meta = get_post_meta( $post->ID );
?>
<p>
<span class="prfx-row-title"><?php _e( 'Pap Checkbox', 'prfx-textdomain' )?></span>
<div class="prfx-row-content">
<label for="page_<?php echo $page->ID; ?>">
<input id="page_<?php echo $page->ID; ?>" type="checkbox" name="checkfield[]" value="<?php echo $page->ID; ?>" <?php if ( isset($checkfield [ ?>'<?php echo $page->ID; ?>'<?php ] ) ) checked( $checkfield[ ?>'<?php echo $page->ID; ?>'<?php ][0], '<?php echo $page->ID; ?>'); ?>/> <?php _e( 'Check if this page is Papiamento', 'prfx-textdomain' )?></label> <br>
</div>
</p>
<?php
I've been trying to make it like that, if Pap, then checked, but if not Pap, unchecked in every page.

If the metabox is saving the checkbox settings properly, you can use the following code to make the check in header.php:
if ( in_array( get_the_ID(), get_post_meta( get_the_ID(), 'checkfield', true ) ) ) {
wp_nav_menu(array( 'theme_location' => 'menu_top_pap' ) );
} else {
wp_nav_menu(array( 'theme_location' => 'secondary-menu' ) );
}

Related

Hello, does anyone know how to add a drop down menu of attributes in Dokan?

This is the code to add a new field in Dokan:
Here is also the link to how it works : https://nayemdevs.com/how-to-add-a-new-field-on-product-upload-form-dokan-multivendor/
Please can someone tell me how to add a drop down menu for attributes instead of a field.
I want to add a drop down menu of specific attributes in Dokan POP UP
Este codigo va en el tema hijo activo .
Function.php
/*
* Adding extra field on New product popup/without popup form
*/
add_action( 'dokan_new_product_after_product_tags','new_product_field',10 );
function new_product_field(){ ?>
<div class="dokan-form-group">
<input type="text" class="dokan-form-control" name="new_field" placeholder="<?php esc_attr_e( 'Product Code', 'dokan-lite' ); ?>">
</div>
<?php
}
/*
* Saving product field data for edit and update
*/
add_action( 'dokan_new_product_added','save_add_product_meta', 10, 2 );
add_action( 'dokan_product_updated', 'save_add_product_meta', 10, 2 );
function save_add_product_meta($product_id, $postdata){
if ( ! dokan_is_user_seller( get_current_user_id() ) ) {
return;
}
if ( ! empty( $postdata['new_field'] ) ) {
update_post_meta( $product_id, 'new_field', $postdata['new_field'] );
}
}
/*
* Showing field data on product edit page
*/
add_action('dokan_product_edit_after_product_tags','show_on_edit_page',99,2);
function show_on_edit_page($post, $post_id){
$new_field = get_post_meta( $post_id, 'new_field', true );
?>
<div class="dokan-form-group">
<input type="hidden" name="new_field" id="dokan-edit-product-id" value="<?php echo esc_attr( $post_id ); ?>"/>
<label for="new_field" class="form-label"><?php esc_html_e( 'Product Code', 'dokan-lite' ); ?></label>
<?php dokan_post_input_box( $post_id, 'new_field', array( 'placeholder' => __( 'product code', 'dokan-lite' ), 'value' => $new_field ) ); ?>
<div class="dokan-product-title-alert dokan-hide">
<?php esc_html_e( 'Please enter product code!', 'dokan-lite' ); ?>
</div>
</div> <?php
}
// showing on single product page
add_action('woocommerce_single_product_summary','show_product_code',13);
function show_product_code(){
global $product;
if ( empty( $product ) ) {
return;
}
$new_field = get_post_meta( $product->get_id(), 'new_field', true );
if ( ! empty( $new_field ) ) {
?>
<span class="details"><?php echo esc_attr__( 'Product Code:', 'dokan-lite' ); ?> <strong><?php echo esc_attr( $new_field ); ?></strong></span>
<?php
}
}

Wordpress settings page options not updating backend

I'm developing a Wordpress plugin, and I have been basing my admin page development off of the Codex's custom settings page example. I have been unable to get it to update the setting in the wp_options db, and I can't seem to find a good explanation as to what I've done wrong. When I click 'save settings', the page refreshes and a bar pops up at the top saying the settings were saved properly, but nothing shows up in wp_options, and the text input box isn't populated with the updated value.
Here's what I have:
Admin init section:
function secg_settings_init() {
register_setting( 'secg', 'secg_options' );
add_settings_section(
'secg_section_developers',
__( 'API Keys.', 'secg' ),
'secg_section_developers_cb',
'secg'
);
add_settings_field(
'secg_field_test_secret_key',
__( 'test-secret-key', 'secg' ),
'secg_field_test_secret_key_cb',
'secg',
'secg_section_developers',
[
'label_for' => 'secg_field_test_secret_key',
'id' => 'test-secret-key'
]
);
}
add_action( 'admin_init', 'secg_settings_init' );
Section and field Callbacks:
function secg_section_developers_cb( $args ) {
?>
<p id="<?php echo esc_attr( $args['id'] ); ?>"><?php esc_html_e( 'Section Developers Cb.', 'secg' ); ?></p>
<?php
}
function secg_field_test_secret_key_cb( $args ) {
$options = secg_option();
?>
<input type="text"
name="secg_options[<?php echo esc_attr( $args['label_for'] ); ?>]"
id="<?php echo esc_attr( $args['label_for'] ); ?>"
value="<?php $options[ $args['label_for'] ] ?>" />
<?php
}
Add Page:
function secg_options_page() {
add_menu_page(
'My Plugin',
'Plugin Options',
'manage_options',
'secg',
'secg_options_page_html'
);
}
add_action( 'admin_menu', 'secg_options_page' );
Handler for setting default values:
function secg_option() {
$default = array(
'secg_field_test_secret_key' => 'test secret key'
);
return get_option('secg_options', $default);
}
And finally, the page itself:
function secg_options_page_html() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if ( isset( $_GET['settings-updated'] ) ) {
add_settings_error( 'secg_messages', 'secg_message', __( 'Settings Saved', 'secg' ), 'updated' );
}
settings_errors( 'secg_messages' );
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<form action="options.php" method="post">
<?php
settings_fields( 'secg' );
do_settings_sections( 'secg' );
submit_button( 'Save Settings' );
?>
</form>
</div>
<?php
}
Thanks!

How can I make an item show/hide based on a checkbox being checked/not checked?

So I created a checkbox on my wordpress theme. everything saves properly and all of that:
function member_page_featured_meta() {
add_meta_box( 'member_page_meta', __( 'Is this a Member Page?', 'member_page-textdomain' ), 'member_page_meta_callback', 'page', 'side', 'low' );
}
add_action( 'add_meta_boxes', 'member_page_featured_meta' );
/**
* Outputs the content of the meta box
*/
function member_page_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'member_page_nonce' );
$member_page_stored_meta = get_post_meta( $post->ID );
?>
<p>
<div class="member_page-row-content">
<label for="featured-checkbox">
<input type="checkbox" name="featured-checkbox" id="featured-checkbox" value="yes" <?php if ( isset ( $member_page_stored_meta['featured-checkbox'] ) ) checked( $member_page_stored_meta['featured-checkbox'][0], 'yes' ); ?> />
<?php _e( 'Yes', 'member_page-textdomain' )?>
</label>
</div>
</p>
<?php
}
/**
* Saves the custom meta input
*/
function member_page_meta_save( $post_id ) {
// Checks save status - overcome autosave, etc.
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'member_page_nonce' ] ) && wp_verify_nonce( $_POST[ 'member_page_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and saves - save checked as yes and unchecked at no
if( isset( $_POST[ 'featured-checkbox' ] ) ) {
update_post_meta( $post_id, 'featured-checkbox', 'yes' );
} else {
update_post_meta( $post_id, 'featured-checkbox', 'no' );
}
}
add_action( 'save_post', 'member_page_meta_save' );
What I need is to make it so that I can use this to show/hide content in Wordpress using Php, but I don't exactly know how to target it in a php if statement.
Do I use the name (featured-checkbox), id (#featured-checkbox), fuction name ($member_page_featured_meta), etc.
And then to make that a specific bit of HTML, how would I want to go about doing that?
HTML/PHP that I am trying to hide when the box is checked.
<header class="entry-header">
<div style="float:right;">
<div id="membership" "class='false' if not member"></div>
<div id="phone-answering" "class='false' if not phone client"></div>
<div id="location-icon" "class='false' if only one location"></div>
</div>
<?php the_title( '<h1 class="entry-title" style="clear:none; font-size: 3em; text-decoration: underline;">', '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="entry-content">
<div id="tabs">
<ul class="tabs">
<li>Key Intake</li>
<li>Business Facts</li>
<li>Common Answers</li>
<li>Understanding</li>
<li>Current</li>
<li>Locations</li>
<li>Notify</li>
</ul>
<div id="key-intake" class="">
<h1 class="tabtitle">Key Intake</h1>
<?php
// Retrieves the stored value from the database
$key_intake_meta_value = get_post_meta( get_the_ID(), 'Key Intake', true );
// Checks and displays the retrieved value
if( !empty( $key_intake_meta_value ) ) {
echo $key_intake_meta_value;
} else {
echo 'Value Not Fount or Empty';
}
?>
</div>
<div id="business-facts">
<h1 class="tabtitle">Business Facts</h1>
<?php
// Retrieves the stored value from the database
$business_facts_meta_value = get_post_meta( get_the_ID(), 'Business Facts', true );
// Checks and displays the retrieved value
if( !empty( $business_facts_meta_value ) ) {
echo $business_facts_meta_value;
} else {
echo 'Value Not Fount or Empty';
}
?>
</div>
<div id="common-answers">
<h1 class="tabtitle">Common Answers</h1>
<?php
// Retrieves the stored value from the database
$common_answers_meta_value = get_post_meta( get_the_ID(), 'Common Answers', true );
// Checks and displays the retrieved value
if( !empty( $common_answers_meta_value ) ) {
echo $common_answers_meta_value;
} else {
echo 'Value Not Fount or Empty';
}
?>
</div>
<div id="understanding">
<h1 class="tabtitle">Understanding</h1>
<?php
// Retrieves the stored value from the database
$understanding_meta_value = get_post_meta( get_the_ID(), 'Understanding', true );
// Checks and displays the retrieved value
if( !empty( $understanding_meta_value ) ) {
echo $understanding_meta_value;
} else {
echo 'Value Not Fount or Empty';
}
?>
</div>
<div id="current">
<h1 class="tabtitle">Current</h1>
<?php
// Retrieves the stored value from the database
$current_meta_value = get_post_meta( get_the_ID(), 'Current', true );
// Checks and displays the retrieved value
if( !empty( $current_meta_value ) ) {
echo $current_meta_value;
} else {
echo 'Value Not Fount or Empty';
}
?>
</div>
<div id="location">
<h1 class="tabtitle">Locations</h1>
<?php
// Retrieves the stored value from the database
$locations_meta_value = get_post_meta( get_the_ID(), 'Locations', true );
// Checks and displays the retrieved value
if( !empty( $locations_meta_value ) ) {
echo $locations_meta_value;
} else {
echo 'Value Not Fount or Empty';
}
?>
</div>
<div id="notify">
<h1 class="tabtitle">Notify</h1>
<?php
// Retrieves the stored value from the database
$notify_meta_value = get_post_meta( get_the_ID(), 'Notify', true );
// Checks and displays the retrieved value
if( !empty( $notify_meta_value ) ) {
echo $notify_meta_value;
} else {
echo 'Value Not Fount or Empty';
}
?>
</div>
</div>
<?php
the_content();
wp_link_pages( array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'odextranet' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
I would use an inline style to hide and show. You can't target HTML from the PHP so the basic process on the theme side would be:
Decide if it should be shown or not.
If it should be hidden use a variable to store the inline CSS used to hide the section.
Output that variable in the container tag for the section that contains the data to hide or show.
Here is an example that uses some of your code:
<?php
// Retrieves the stored value from the database
$current_meta_value = get_post_meta( get_the_ID(), 'Current', true );
//Will either be empty or contain the inline CSS to hide the content.
$hideOrShow = "";
// Checks and displays the retrieved value
if( !empty( $current_meta_value ) ) {
$hideOrShow = "display: none;";
}
?>
<div id="content_to_hide_or_show" style="<?php echo $hideOrShow; ?>">
<!--Content that will be hidden or shown -->
</div>

Wordpress metabox drop down list isn't saving

I have 2 custom post types named clients and casestudies. I'm trying to build a meta box on the clients post type that will have a drop down list featuring the titles of all posts from the casestudies post type. This will end up with a page displaying the featured image from the clients post type, then hyperlink off to the relevant casestudies post if a selection is made from the drop down list.
I have followed this tutorial to get a meta box put together: http://code.tutsplus.com/tutorials/how-to-create-custom-wordpress-writemeta-boxes--wp-20336
This is the meta box code I have in my functions.php file:
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'clients', 'side', 'default' );
}
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : ”;
?>
<p>
<label for="my_meta_box_select">Select which case study this logo will link to when it is clicked:<br /><br /></label>
<select name="my_meta_box_select" id="my_meta_box_select" style="width:100%;">
<option value="No case study">No case study</option>
<?php
$casestudies = array( 'post_type' => 'casestudies', 'orderby' => 'title', 'order' => 'asc', );
$casestudiesloop = new WP_Query( $casestudies );
while ( $casestudiesloop->have_posts() ) : $casestudiesloop->the_post();
?> <option value="<?php the_title(); ?>" <?php selected( $selected, $casestudies['the_title'] ); ?> ><?php the_title(); ?></option>
<?php
endwhile;
?>
</select>
</p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
if( !current_user_can( 'edit_post', $post_id ) ) return;
if( isset( $_POST['my_meta_box_select'] ) )
update_post_meta( $post_id, 'my_meta_box_select', esc_attr( $_POST['my_meta_box_select'] ) );
}
The meta box displays correctly on the correct post type, but when I update the post it won't save the data.
Thanks.
You don't have nonce hidden field. Save function would return nothing.
<input type="hidden" name="meta_box_nonce" id="meta_box_nonce" value="<?php echo wp_create_nonce( 'my_meta_box_nonce' ); ?>" />
UPDATE:
So your cd_meta_box_cb function would be
<?php
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : ”;
?>
<p>
<label for="my_meta_box_select">Select which case study this logo will link to when it is clicked:<br /><br /></label>
<select name="my_meta_box_select" id="my_meta_box_select" style="width:100%;">
<option value="No case study">No case study</option>
<?php
$casestudies = array( 'post_type' => 'casestudies', 'orderby' => 'title', 'order' => 'asc', );
$casestudiesloop = new WP_Query( $casestudies );
while ( $casestudiesloop->have_posts() ) : $casestudiesloop->the_post();
?> <option value="<?php the_title(); ?>" <?php selected( $selected, $casestudies['the_title'] ); ?> ><?php the_title(); ?></option>
<?php
endwhile;
?>
</select>
</p>
<input type="hidden" name="meta_box_nonce" id="meta_box_nonce" value="<?php echo wp_create_nonce( 'my_meta_box_nonce' ); ?>" />
<?php
}
?>

Wordpress Custom Meta Box Returns Empty?

I created a custom meta box (author) for a custom post type (books). The box shows up in the admin fine, but I can't figure out how to display the author in my theme.
This is the code used to create the box:
/**
* Adds a meta box to the post editing screen
*/
function prfx_custom_meta() {
add_meta_box( 'prfx_meta', __( 'Book Author', 'prfx-textdomain' ), 'prfx_meta_callback', 'rabe_books', 'side' );
}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );
/**
* Outputs the content of the meta box
*/
function prfx_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID );
?>
<p>
<label for="meta-text" class="prfx-row-title"><?php _e( 'Example Text Input', 'prfx-textdomain' )?></label>
</p>
<?php
}
/**
* Saves the custom meta input
*/
function prfx_meta_save( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'meta-text' ] ) ) {
update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) );
}
}
add_action( 'save_post', 'prfx_meta_save' );
/**
* Adds the meta box stylesheet when appropriate
*/
function prfx_admin_styles(){
global $typenow;
if( $typenow == 'post' ) {
wp_enqueue_style( 'prfx_meta_box_styles', plugin_dir_url( __FILE__ ) . 'meta-box-styles.css' );
}
}
add_action( 'admin_print_styles', 'prfx_admin_styles' ); ?>
And this is what I placed in my template:
<div class="book-author">by:
<?php $book_author = get_post_meta( get_the_ID(), 'meta-text', true );
if (!empty($book_author)) {
echo $book_author;
} elseif (empty($book_author)) {
echo "Why doesnt it work?";
} ?>
</div>
Instead of displaying the author, it displays the text "Why doesnt it work?" which I guess means that the value is empty. But it shouldn't be. What am I doing wrong?
As mevius mentioned, you're issue is here:
<?php _e( 'Example Text Input', 'prfx-textdomain' )?>
http://codex.wordpress.org/Function_Reference/_e
You'll need to assign the box to a data point.
This is a snippet from one of our functions.php file that has this:
function css_metadata_box($object, $box) { ?>
<p>
<label for="custom-css-data">Add custom CSS for this page</label>
<textarea name="custom-css-data" id="custom-css-data" cols="60" rows="25" tabindex="30" style="width: 97%;"><?php
echo esc_html( get_post_meta( $object->ID, 'custom-css', true ), 1 );
?></textarea>
<input type="hidden" name="css_meta_box_nonce" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
</p>
<?php }
add_meta_box('css-data-box', 'Page CSS', 'css_metadata_box', 'page', 'normal', 'high');

Categories