Why doesnt this code display a basic wordpress settings page - php

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

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 multiple select in theme option page

im trying to make this work. its theme option page that list categories and allow you to do MULTIPLE select
but something is wrong and it not saving the values and in the end how can i show these selected categories in index?
here is what i found in web and sof
<?php
add_action( 'admin_init', 'theme_options_init' );
add_action( 'admin_menu', 'theme_options_add_page' );
function theme_options_init(){
register_setting( 'sample_options', 'sample_theme_options', 'theme_options_validate' );
}
function theme_options_add_page() {
add_theme_page( __( 'Theme Options', 'sampletheme' ), __( 'Theme Options', 'sampletheme' ), 'edit_theme_options', 'theme_options', 'theme_options_do_page' );
}
function theme_options_do_page() {
global $select_options, $radio_options;
if ( ! isset( $_REQUEST['settings-updated'] ) )
$_REQUEST['settings-updated'] = false;
?>
<div class="wrap">
<?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme Options', 'sampletheme' ) . "</h2>"; ?>
<?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
<div class="updated fade"><p><strong><?php _e( 'Options saved', 'sampletheme' ); ?></strong></p></div>
<?php endif; ?>
<form method="post" action="options.php">
<?php settings_fields( 'sample_options' ); ?>
<?php $options = get_option( 'sample_theme_options' ); ?>
<select multiple="multiple" name="site_options[categorychoice][]">
<?php $option = get_option('site_options'); ?>
<?php
$args = array(
'orderby' => 'name',
'parent' => 0,
'exclude' => 1
);
$categories = get_categories( $args );
foreach ($categories as $category) { ?>
<?php $selected = in_array( $category->cat_ID, $option['categorychoice'] ) ? ' selected="selected" ' : ''; ?>
<option value="<?php echo $category->term_id; ?>" <?php echo $selected; ?> >
<?php echo $category->cat_name . ' (' . $category->category_count .')'; ?>
</option>
<?php } //endforeach ?>
</select>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'sampletheme' ); ?>" />
</p>
</form>
</div>
<?php
}
function theme_options_validate( $input ) {
global $select_options, $radio_options, $categories;
if ( ! array_key_exists( $input['categorychoice'], $categories ) )
$input['categorychoice'] = NULL;
return $input;
}

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!

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