on creating custom field in option page for my wordpress theme it show some error on each input type text call_user_func() expects parameter 1 to be a valid callback,class 'MySettingsPage' does not have a method logo_callback() or social_callback() please suggest some solution
private $options;
public function __construct()
{
add_action( 'admin_menu', array( $this, 'bguru_register_options_page' ) );
add_action( 'admin_init', array( $this, 'bguru_register_settings' ) );
}
public function bguru_register_options_page()
{
// This page will be under "Settings"
add_theme_page('Business Guru Options',
'Theme Customizer',
'edit_theme_options',
'bguru-options',
array( $this, 'bguru_options_page')
);
}
public function bguru_options_page()
{
// Set class property
$this->options = get_option( 'bguru_logo' );
$this->options = get_option( 'bguru_vimeo' );
?>
<div class="wrap">
<?php screen_icon(); ?>
<h1>Business Guru Options</h1>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'defaultbg' );
do_settings_sections( 'defaultbg' );
submit_button();
?>
</form>
</div>
<?php
}
public function bguru_register_settings()
{
register_setting('defaultbg','bguru_logo', array( $this, 'sanitize' ) );
register_setting('defaultbg', 'bguru_vimeo', array( $this, 'sanitize' ));
add_settings_section(
'setting_section_id', // ID
'General',
array( $this, 'print_section_info' ), // Callback
'defaultbg' // Page
);
add_settings_field(
'bguru_logo', // ID
'Logo', // Title
array($this,'logo_callback()' ), // Callback
'defaultbg', // Page
'setting_section_id' // Section
);
add_settings_field(
'bguru_vimeo', // ID
'Vimeo', // Vimeo
array( $this, 'socialv_callback()' ), // Callback
'defaultbg', // Page
'setting_section_id' // Section
);
}
public function sanitize( $input )
{
$new_input = array();
if( isset( $input['bguru_logo'] ) )
$new_input['bguru_logo'] = sanitize_text_field( $input['bguru_logo'] );
if( isset( $input['bguru_vimeo'] ) )
$new_input['bguru_vimeo'] = sanitize_text_field( $input['bguru_vimeo'] );
return $new_input;
}
public function print_section_info()
{
print 'Enter your settings below:';
}
public function logo_callback()
{
printf(`enter code here`
'<input type="text" id="bguru_logo" size="50" name="bguru_logo" value="%s" />',
isset( $this->options['bguru_logo'] ) ? esc_attr( $this->options['bguru_logo']) : ''
);
}
public function socialv_callback()
{
printf(
'<input type="text" id="bguru_vimeo" size="50" name="bguru_vimeo" value="%s" />',
isset( $this->options['bguru_vimeo'] ) ? esc_attr( $this->options['bguru_vimeo']) : ''
);
}
}
if( is_admin() )
$my_settings_page = new MySettingsPage();
After Look Into Your Code, I think You need to remove () From array($this,'logo_callback()' ).
See below code, i have change & fixed:-
class MySettingsPage
{
private $options;
public function __construct()
{
add_action( 'admin_menu', array( $this, 'bguru_register_options_page' ) );
add_action( 'admin_init', array( $this, 'bguru_register_settings' ) );
}
public function bguru_register_options_page()
{
// This page will be under "Settings"
add_theme_page('Business Guru Options',
'Theme Customizer',
'edit_theme_options',
'bguru-options',
array( $this, 'bguru_options_page')
);
}
public function bguru_options_page()
{
// Set class property
$this->options['bguru_logo'] = get_option( 'bguru_logo' );
$this->options['bguru_vimeo'] = get_option( 'bguru_vimeo' );
?>
<div class="wrap">
<?php screen_icon(); ?>
<h1>Business Guru Options</h1>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'defaultbg' );
do_settings_sections( 'defaultbg' );
submit_button();
?>
</form>
</div>
<?php
}
public function bguru_register_settings()
{
register_setting('defaultbg','bguru_logo', array( $this, 'sanitize' ) );
register_setting('defaultbg', 'bguru_vimeo', array( $this, 'sanitize' ));
add_settings_section(
'setting_section_id', // ID
'General',
array( $this, 'print_section_info' ), // Callback
'defaultbg' // Page
);
add_settings_field(
'bguru_logo', // ID
'Logo', // Title
array($this,'logo_callback' ), // Callback
'defaultbg', // Page
'setting_section_id' // Section
);
add_settings_field(
'bguru_vimeo', // ID
'Vimeo', // Vimeo
array( $this, 'socialv_callback' ), // Callback
'defaultbg', // Page
'setting_section_id' // Section
);
}
public function sanitize( $input )
{
$new_input = array();
if( isset( $input['bguru_logo'] ) )
$new_input['bguru_logo'] = sanitize_text_field( $input['bguru_logo'] );
if( isset( $input['bguru_vimeo'] ) )
$new_input['bguru_vimeo'] = sanitize_text_field( $input['bguru_vimeo'] );
return $new_input;
}
public function print_section_info()
{
print 'Enter your settings below:';
}
public function logo_callback()
{
printf('<input type="text" id="bguru_logo" size="50" name="bguru_logo" value="%s" />',
isset( $this->options['bguru_logo'] ) ? esc_attr( $this->options['bguru_logo']) : ''
);
}
public function socialv_callback()
{
printf('<input type="text" id="bguru_vimeo" size="50" name="bguru_vimeo" value="%s" />',
isset( $this->options['bguru_vimeo'] ) ? esc_attr( $this->options['bguru_vimeo']) : ''
);
}
}
if( is_admin() )
$my_settings_page = new MySettingsPage();
Hope this will work for you.
Related
I have to create an option page using array in my wordpress theme. after submitting all fill up information removed while i am using
var_dump( $this->options) show blank array(){}
I want when submitting option page form submit information show in the input box and i will get_option value in some variable so that i have to display the information in my theme.
<?php
class MySettingsPage
{
private $options;
public function __construct()
{
add_action( 'admin_menu', array( $this, 'bguru_register_options_page' ) );
add_action( 'admin_init', array( $this, 'bguru_register_settings' ) );
}
public function bguru_register_options_page()
{
// This page will be under "Settings"
add_theme_page('Business Guru Options',
'Theme Customizer',
'edit_theme_options',
'bguru-options',
array( $this, 'bguru_options_page')
);
}
public function bguru_options_page()
{
// Set class property
$bguru_logo = is_array( get_option( 'bguru_logo' ) ) ? get_option( 'bguru_logo' ) : array();
$bguru_vimeo = is_array( get_option( 'bguru_vimeo' ) ) ? get_option( 'bguru_vimeo' ) : array();
$this->options = array_merge( $bguru_logo, $bguru_vimeo);
var_dump($this->options);
?>
<div class="wrap">
<?php screen_icon(); ?>
<h1>Business Guru Options</h1>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'defaultbg' );
do_settings_sections( 'defaultbg' );
submit_button();
?>
</form>
</div>
<?php
}
public function bguru_register_settings()
{
register_setting('defaultbg','bguru_logo', array( $this, 'sanitize' ) );
register_setting('defaultbg', 'bguru_vimeo', array( $this, 'sanitize' ));
add_settings_section(
'setting_section_id', // ID
'General',
array( $this, 'print_section_info' ), // Callback
'defaultbg' // Page
);
add_settings_field(
'bguru_logo', // ID
'Logo', // Title
array($this,'logo_callback' ), // Callback
'defaultbg', // Page
'setting_section_id' // Section
);
add_settings_field(
'bguru_vimeo', // ID
'Vimeo', // Vimeo
array( $this, 'socialv_callback' ), // Callback
'defaultbg', // Page
'setting_section_id' // Section
);
}
public function sanitize( $input )
{
$new_input = array();
if( isset( $input['bguru_logo'] ) )
$new_input['bguru_logo'] = sanitize_text_field( $input['bguru_logo'] );
if( isset( $input['bguru_vimeo'] ) )
$new_input['bguru_vimeo'] = sanitize_text_field( $input['bguru_vimeo'] );
return $new_input;
}
public function print_section_info()
{
print 'Enter your settings below:';
}
public function logo_callback()
{
printf('<input type="text" id="bguru_logo" size="50" name="bguru_logo" value="%s" />',
isset( $this->options['bguru_logo'] ) ? esc_attr( $this->options['bguru_logo']) : ''
);
}
public function socialv_callback()
{
printf('<input type="text" id="bguru_vimeo" size="50" name="bguru_vimeo" value="%s" />',
isset( $this->options['bguru_vimeo'] ) ? esc_attr( $this->options['bguru_vimeo']) : ''
);
}
}
if( is_admin() )
$my_settings_page = new MySettingsPage();
I have a custom post type speaker and in speaker I have a custom field speaker_organization. How can I validate this and display error in admin notice.
add_action( 'add_meta_boxes', 'speaker_organization_box' );
function speaker_organization_box() {
add_meta_box(
'speaker_organization',
__( 'Organization', 'dbem' ),
'speaker_organization_box_content',
'speaker',
'side',
'high'
);
}
function speaker_organization_box_content( $post ) {
// generate a nonce field
wp_nonce_field( basename( __FILE__ ), 'dbem-speaker-organization-nonce' );
// get previously saved meta values (if any)
$speaker_organization = get_post_meta( $post->ID, 'speaker_organization', true );
echo '<label for="speaker_organization"></label>';
echo '<input type="text" id="speaker_organization" name="speaker_organization" placeholder="Organization Name" value="'.$speaker_organization.'" />';
}
function speaker_organization_box_save( $post_id ) {
$speaker_organization = $_POST['speaker_organization'];
update_post_meta( $post_id, 'speaker_organization', $speaker_organization );
}
add_action( 'save_post', 'speaker_organization_box_save' );
using
add_action( 'admin_notices', 'my_admin_notice' );
or using validate js
add_action('admin_enqueue_scripts', 'add_my_js');
function add_my_js(){
wp_enqueue_script('my_validate', 'path/to/jquery.validate.min.js', array('jquery'));
wp_enqueue_script('my_script_js', 'path/to/my_script.js');
}
jQuery().ready(function() {
jQuery("#post").validate();
});
<input type="text" name="my_custom_text_field" class="required"/>
function wpse_update_post_custom_values($post_id, $post) {
// Do some checking...
if($_POST['subhead'] != 'value i expect') {
// Add an error here
$errors->add('oops', 'There was an error.');
}
return $errors;
}
add_action('save_post','wpse_update_post_custom_values',1,2);
validate js code
using condition + admin notice
I have a Wordpress plugin settings page which uses the following plugin settings page:
<?php
class NewPlugin
{
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'New Plugin',
'manage_options',
'new-plugin-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'new_plugin_options' );
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>New Plugin Settings</h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'new_plugin_options_group' );
do_settings_sections( 'new-plugin-admin' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'new_plugin_options_group', // Option group
'new_plugin_options', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'setting_section_id', // ID
'Settings', // Title
array( $this, 'print_section_info' ), // Callback
'new-plugin-admin' // Page
);
add_settings_field(
'title',
'Title',
array( $this, 'title_callback' ),
'new-plugin-admin',
'setting_section_id'
);
add_settings_field(
'addresslineone',
'Address Line One',
array( $this, 'addresslineone_callback' ),
'new-plugin-admin',
'setting_section_id'
);
add_settings_field(
'addresslinetwo',
'Address Line Two',
array( $this, 'addresslinetwo_callback' ),
'new-plugin-admin',
'setting_section_id'
);
}
/**
* Sanitize each setting field as needed
*
* #param array $input Contains all settings fields as array keys
*/
public function sanitize( $input )
{
if( isset( $input['title'] ) )
$new_input['title'] = sanitize_text_field( $input['title'] );
if( isset( $input['addresslineone'] ) )
$new_input['addresslineone'] = sanitize_text_field( $input['addresslineone'] );
if( isset( $input['addresslinetwo'] ) )
$new_input['addresslinetwo'] = sanitize_text_field( $input['addresslinetwo'] );
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info()
{
print 'Enter your settings below:';
}
/**
* Get the settings option array and print one of its values
*/
public function title_callback()
{
printf(
'<input type="text" id="title" name="new_plugin_options[title]" value="%s" />',
isset( $this->options['title'] ) ? esc_attr( $this->options['title']) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function addresslineone_callback()
{
printf(
'<input type="text" id="addresslineone" name="new_plugin_options[addresslineone]" value="%s" />',
isset( $this->options['addresslineone'] ) ? esc_attr( $this->options['addresslineone']) : ''
);
}
public function addresslinetwo_callback()
{
printf(
'<input type="text" id="addresslinetwo" name="new_plugin_options[addresslinetwo]" value="%s" />',
isset( $this->options['addresslinetwo'] ) ? esc_attr( $this->options['addresslinetwo']) : ''
);
}
}
if( is_admin() )
$new_plugin_settings = new NewPlugin();
I already had a widget where the details asked for are in an array "$instance". I am trying to write an if statement which asks if $instance exists, then convert the strings to the new array.. So far I have not had much luck. Can anyone point me in the right direction?
As there is not much information about the $instance variable, these functions should help depending on the condition to consider that a variable exists :
isset
empty
property_exists
I am looking at building a plugin with a admin settings page. Looking at the codex, I have the following code:
<?php
class MyNewPlugin
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'My New Plugin',
'manage_options',
'my-new-plugin-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'my_new_plugin_option' );
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>My New Plugin Settings</h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'my_new_plugin_option_group' );
do_settings_sections( 'my-new-plugin-admin' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'my_new_plugin_option_group', // Option group
'my_new_plugin_option', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'setting_section_id', // ID
'Settings', // Title
array( $this, 'print_section_info' ), // Callback
'my-new-plugin-admin' // Page
);
add_settings_field(
'title',
'Title',
array( $this, 'title_callback' ),
'my-new-plugin-admin',
'setting_section_id'
);
add_settings_field(
'addresslineone',
'Address Line One',
array( $this, 'addresslineone_callback' ),
'my-new-plugin-admin',
'setting_section_id'
);
add_settings_field(
'addresslinetwo',
'Address Line Two',
array( $this, 'addresslinetwo_callback' ),
'my-new-plugin-admin',
'setting_section_id'
);
}
/**
* Sanitize each setting field as needed
*
* #param array $input Contains all settings fields as array keys
*/
public function sanitize( $input )
{
if( isset( $input['title'] ) )
$new_input['title'] = sanitize_text_field( $input['title'] );
if( isset( $input['addresslineone'] ) )
$new_input['addresslineone'] = sanitize_text_field( $input['addresslineone'] );
if( isset( $input['addresslinetwo'] ) )
$new_input['addresslinetwo'] = sanitize_text_field( $input['addresslinetwo'] );
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info()
{
print 'Enter your settings below:';
}
/**
* Get the settings option array and print one of its values
*/
public function title_callback()
{
printf(
'<input type="text" id="title" name="my_new_plugin_option[title]" value="%s" />',
isset( $this->options['title'] ) ? esc_attr( $this->options['title']) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function addresslineone_callback()
{
printf(
'<input type="text" id="addresslineone" name="my_new_plugin_option[addresslineone]" value="%s" />',
isset( $this->options['addresslineone'] ) ? esc_attr( $this->options['addresslineone']) : ''
);
}
public function addresslinetwo_callback()
{
printf(
'<input type="text" id="addresslinetwo" name="my_new_plugin_option[addresslinetwo]" value="%s" />',
isset( $this->options['addresslinetwo'] ) ? esc_attr( $this->options['addresslinetwo']) : ''
);
}
}
if( is_admin() )
$my_new_plugin_settings = new MyNewPlugin();
include_once "mynewpluginwidget.php";
A simple plugin to start off with, the user enters the settings on the setting page and it displays in the widget area (user has to add the widget where they want it to be).
The question is, how do I extract the settings and display them? I presume they are in the array $this, but cannot get them to display..
For my Wordpress plugin option page I want to use checkboxes to set the options. I'm using this code to add it to the option page and it works well. Except that I want the checkboxes default set to unchecked. How do I do this?
public function id_number_callback()
{
printf(
'<input id="%1$s" name="cus_func_option[%1$s]" type="checkbox" %2$s" />',
'add_image_dimesions',
checked( isset( $this->options['add_image_dimesions'] ), true, false )
);
Edit: The full code I want to use is this
class MySettingsPage
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'Custom functions',
'manage_options',
'cus-func-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'cus_func_option' );
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Custom Functions</h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'cus_func_group' );
do_settings_sections( 'cus-func-admin' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'cus_func_group', // Option group
'cus_func_option', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'setting_section_id', // ID
'Which Custom Functions do you want to use?', // Title
array( $this, 'print_section_info' ), // Callback
'cus-func-admin' // Page
);
add_settings_field(
'add_image_dimesions', // ID
'Add image dimesions to the Media Page', // Title
array( $this, 'id_number_callback' ), // Callback
'cus-func-admin', // Page
'setting_section_id' // Section
);
add_settings_field(
'title',
'Title',
array( $this, 'title_callback' ),
'cus-func-admin',
'setting_section_id'
);
}
/**
* Sanitize each setting field as needed
*
* #param array $input Contains all settings fields as array keys
*/
public function sanitize( $input )
{
$new_input = array();
if( isset( $input['add_image_dimesions'] ) )
$new_input['add_image_dimesions'] = absint( $input['add_image_dimesions'] );
if( isset( $input['title'] ) )
$new_input['title'] = sanitize_text_field( $input['title'] );
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info()
{
print 'Pick your Custom Functions below:';
}
/**
* Get the settings option array and print one of its values
*/
public function id_number_callback()
{
printf(
'<input id="%1$s" name="cus_func_option[%1$s]" type="checkbox" %2$s" />',
'add_image_dimesions',
checked( isset( $this->options['add_image_dimesions'] ), true, false )
);
}
/**
* Get the settings option array and print one of its values
*/
public function title_callback()
{
printf(
'<input id="%1$s" name="cus_func_option[%1$s]" type="checkbox" %2$s />',
'title',
checked( isset( $this->options['title'] ), true, false )
);
}
}
if( is_admin() )
$my_settings_page = new MySettingsPage();
Refer to the WordPress Documentation for checked
What is the value of isset( $this->options['add_image_dimesions'] ) in your code?
This is what is determining whether it's checked or not.
If you want the checkbox to always be unchecked when the page is loaded, then you can just remove the second printf token, %2$2, and the second argument to printf, checked( isset( $this->options['add_image_dimesions'] ), true, false ), which will give you this:
public function id_number_callback()
{
printf(
'<input id="%1$s" name="cus_func_option[%1$s]" type="checkbox" />',
'add_image_dimesions'
);