Wordpress settings page options not updating backend - php

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!

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
}
}

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

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' ) );
}

Why doesnt this code display a basic wordpress settings page

I am trying to create a wordpress settings page but the code wont display the content. Im no techy so answers must be as plain as possible. I generated the code and it seems to want to find an options page but I input all the code into the plugin page. Any guidance apreciated thanks.
<?php
/*
Plugin Name: Plugin Name
Plugin URI: http://www.domain.com
Description: Plugin description
Author: S. Hawkins
Version: 1.0
Author URI: http://www.domain.com
*/
add_action( 'admin_menu', 'sppf_add_admin_menu' );
add_action( 'admin_init', 'sppf_settings_init' );
function sppf_add_admin_menu( ) {
add_options_page( 'Power Funnels', 'Power Funnels', 'manage_options', 'power_funnels', 'power_funnels_options_page' );
}
function sppf_settings_init( ) {
register_setting( 'pluginPage', 'sppf_settings' );
add_settings_section(
'sppf_pluginPage_section',
__( 'Your section description', 'sppf' ),
'sppf_settings_section_callback',
'pluginPage'
);
add_settings_field(
'sppf_radio_field_0',
__( 'Settings field description', 'sppf' ),
'sppf_radio_field_0_render',
'pluginPage',
'sppf_pluginPage_section'
);
add_settings_field(
'sppf_checkbox_field_1',
__( 'Settings field description', 'sppf' ),
'sppf_checkbox_field_1_render',
'pluginPage',
'sppf_pluginPage_section'
);
add_settings_field(
'sppf_checkbox_field_2',
__( 'Settings field description', 'sppf' ),
'sppf_checkbox_field_2_render',
'pluginPage',
'sppf_pluginPage_section'
);
add_settings_field(
'sppf_select_field_3',
__( 'Settings field description', 'sppf' ),
'sppf_select_field_3_render',
'pluginPage',
'sppf_pluginPage_section'
);
add_settings_field(
'sppf_text_field_4',
__( 'Settings field description', 'sppf' ),
'sppf_text_field_4_render',
'pluginPage',
'sppf_pluginPage_section'
);
add_settings_field(
'sppf_textarea_field_5',
__( 'Settings field description', 'sppf' ),
'sppf_textarea_field_5_render',
'pluginPage',
'sppf_pluginPage_section'
);
}
function sppf_radio_field_0_render( ) {
$options = get_option( 'sppf_settings' );
?>
<input type='radio' name='sppf_settings[sppf_radio_field_0]' <?php checked( $options['sppf_radio_field_0'], 1 ); ?> value='1'>
<?php
}
function sppf_checkbox_field_1_render( ) {
$options = get_option( 'sppf_settings' );
?>
<input type='checkbox' name='sppf_settings[sppf_checkbox_field_1]' <?php checked( $options['sppf_checkbox_field_1'], 1 ); ?> value='1'>
<?php
}
function sppf_checkbox_field_2_render( ) {
$options = get_option( 'sppf_settings' );
?>
<input type='checkbox' name='sppf_settings[sppf_checkbox_field_2]' <?php checked( $options['sppf_checkbox_field_2'], 1 ); ?> value='1'>
<?php
}
function sppf_select_field_3_render( ) {
$options = get_option( 'sppf_settings' );
?>
<select name='sppf_settings[sppf_select_field_3]'>
<option value='1' <?php selected( $options['sppf_select_field_3'], 1 ); ?>Option 1</option>
<option value='2' <?php selected( $options['sppf_select_field_3'], 2 ); ?>Option 2</option>
</select>
<?php
}
function sppf_text_field_4_render( ) {
$options = get_option( 'sppf_settings' );
?>
<input type='text' name='sppf_settings[sppf_text_field_4]' value='<?php echo $options['sppf_text_field_4']; ?>'>
<?php
}
function sppf_textarea_field_5_render( ) {
$options = get_option( 'sppf_settings' );
?>
<textarea cols='40' rows='5' name='sppf_settings[sppf_textarea_field_5]'>
<?php echo $options['sppf_textarea_field_5']; ?>
</textarea>
<?php
}
function sppf_settings_section_callback( ) {
echo __( 'This section description', 'sppf' );
}
function sppf_options_page( ) {
?>
<form action='options.php' method='post'>
<h2>Power Funnels</h2>
<?php
settings_fields( 'pluginPage' );
do_settings_sections( 'pluginPage' );
submit_button();
?>
</form>
<?php
}
?>

Wordpress Custom Options Page is not showing settings fields

I can't get my options page to show any of the added input fields.
add_action( 'admin_menu', 'skd_add_admin_menu' );
add_action( 'admin_init', 'skd_settings_init' );
function skd_add_admin_menu( ) {
add_menu_page( 'skilte-designer', 'Skilte Designer', 'manage_options', 'skilte-designer', 'skilte-designer/skilte-designer-admin.php', plugins_url( '/skilte-designer/img/sign.png' ), 21 );
}
function skd_settings_init( ) {
register_setting( 'pluginPage', 'skd_settings' );
add_settings_section(
'skd_pluginPage_section',
__( 'Your section description', 'wordpress' ),
'skd_settings_section_callback',
'pluginPage'
);
add_settings_field(
'skd_text_field_0',
__( 'Settings field description', 'wordpress' ),
'skd_text_field_0_render',
'pluginPage',
'skd_pluginPage_section'
);
add_settings_field(
'skd_text_field_1',
__( 'Settings field description', 'wordpress' ),
'skd_text_field_1_render',
'pluginPage',
'skd_pluginPage_section'
);
add_settings_field(
'skd_text_field_2',
__( 'Settings field description', 'wordpress' ),
'skd_text_field_2_render',
'pluginPage',
'skd_pluginPage_section'
);
add_settings_field(
'skd_text_field_3',
__( 'Settings field description', 'wordpress' ),
'skd_text_field_3_render',
'pluginPage',
'skd_pluginPage_section'
);
add_settings_field(
'skd_text_field_4',
__( 'Settings field description', 'wordpress' ),
'skd_text_field_4_render',
'pluginPage',
'skd_pluginPage_section'
);
add_settings_field(
'skd_text_field_5',
__( 'Settings field description', 'wordpress' ),
'skd_text_field_5_render',
'pluginPage',
'skd_pluginPage_section'
);
}
function skd_text_field_0_render( ) {
$options = get_option( 'skd_settings' );
?>
<input type='text' name='skd_settings[skd_text_field_0]' value='<?php echo $options['skd_text_field_0']; ?>'>
<?php
}
function skd_text_field_1_render( ) {
$options = get_option( 'skd_settings' );
?>
<input type='text' name='skd_settings[skd_text_field_1]' value='<?php echo $options['skd_text_field_1']; ?>'>
<?php
}
function skd_text_field_2_render( ) {
$options = get_option( 'skd_settings' );
?>
<input type='text' name='skd_settings[skd_text_field_2]' value='<?php echo $options['skd_text_field_2']; ?>'>
<?php
}
function skd_text_field_3_render( ) {
$options = get_option( 'skd_settings' );
?>
<input type='text' name='skd_settings[skd_text_field_3]' value='<?php echo $options['skd_text_field_3']; ?>'>
<?php
}
function skd_text_field_4_render( ) {
$options = get_option( 'skd_settings' );
?>
<input type='text' name='skd_settings[skd_text_field_4]' value='<?php echo $options['skd_text_field_4']; ?>'>
<?php
}
function skd_text_field_5_render( ) {
$options = get_option( 'skd_settings' );
?>
<input type='text' name='skd_settings[skd_text_field_5]' value='<?php echo $options['skd_text_field_5']; ?>'>
<?php
}
function skd_settings_section_callback( ) {
echo __( 'This section description', 'wordpress' );
}
function skd_options_page( ) {
?>
<form action='options.php' method='post'>
<h2>skilte-designer</h2>
<?php
settings_fields( 'pluginPage' );
do_settings_sections( 'pluginPage' );
submit_button();
?>
</form>
<?php
}
The menu item is created, but none of the settings fields that I try to generate above is showing up on the options page.
I can click on the menu item but the page is then empty.
What am I doing wrong here ?
I think you missed position here in add_action( 'admin_menu', 'skd_add_admin_menu' ,13);
I missed to call the option function in add_action skd_options_page()

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