I am making a plugin and when i add a page for managing_option the page is showing me
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘myplugin_options_page’ not found or invalid function name in D:\xampp\htdocs\connect\wp-includes\class-wp-hook.php on line 286
Please find below the code i am using for the plugin
Main Page
<?php
/*
Plugin Name: Name
URI: https://example.com/
Description: abc
Author: Pratik Purohit
License: GPLv2 or later
*/
function myplugin_register_settings() {
add_option( ‘myplugin_option_name’, ‘This is my option value.’);
register_setting( ‘myplugin_options_group’, ‘myplugin_option_name’, ‘myplugin_callback’ );
}
add_action( ‘admin_init’, ‘myplugin_register_settings’ );
function myplugin_register_options_page() {
add_options_page(‘Page Title’, ‘Plugin Menu’, ‘manage_options’, ‘myplugin’, ‘myplugin_options_page’);
}
add_action(‘admin_menu’, ‘myplugin_register_options_page’);
Managing page
<?php function myplugin_options_page()
{
?>
<div>
<?php screen_icon(); ?>
<h2>My Plugin Page Title</h2>
<form method=”post” action=”options.php”>
<?php settings_fields( ‘myplugin_options_group’ ); ?>
<h3>This is my option</h3>
<p>Some text here.</p>
<table>
<tr valign=”top”>
<th scope=”row”><label for=”myplugin_option_name”>Label</label></th>
<td><input type=”text” id=”myplugin_option_name” name=”myplugin_option_name” value=”<?php echo get_option(‘myplugin_option_name’); ?>” /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
} ?>
You need to put your myplugin_options_page function before this line..
add_options_page(‘Page Title’, ‘Plugin Menu’, ‘manage_options’, ‘myplugin’, ‘myplugin_options_page’);
.. because Wordpress is trying to callback your function using the call_user_func_array PHP function, and throws an error because the function has not been found.
If you've defined the myplugin_options_page function in another file, you should add a require (doc) to your main page in order to include your function before the execution of the add_options_page().
Related
I try to create an option page for a Wordpress theme. I manage to create the page, the Metaboxes , but when I send the form, the data are not saved. Before I show you my code , I specify that I use a class to generate the page more easily. Maybe this can guide you to the problem.
In this code I would like to save the "slide" option. This option is registred with register_setting() and visible in the slideshow_metabox() method that displays the form with fields that are named "slide [...]".
add_action('admin_menu', 'add_custom_admin_page');
function add_custom_admin_page(){
add_menu_page('CYBER Auto Pièces', 'CYBER Auto Pièces', 'manage_options', 'cyberautopieces', '' );
add_action( 'admin_init', 'cyberautopieces_register_settings' );
}
function cyberautopieces_register_settings() {
//register our settings
register_setting( 'cyberautopieces-settings', 'slide' );
}
new GeneratePageAdmin ('cyberautopieces','CYBER Auto Pièces','CYBER Auto Pièces', 'manage_options','cyberautopieces');
//Add some metaboxes to the page
add_action('add_meta_boxes','cyberautopieces_admin_metaboxes');
function cyberautopieces_admin_metaboxes(){
add_meta_box('slideshow_box','Slideshow','slideshow_metabox','toplevel_page_cyberautopieces','normal','high');
add_meta_box('save_box','Enregistrer','save_metabox','toplevel_page_cyberautopieces','side','high');
}
//Define the insides of the metabox
function slideshow_metabox(){
var_dump(get_option('slide')); // DISPLAY bool(false) :(
?>
<ul id="slideshow_box">
<li>
<a class="add-image-slider" href="#">
<div class="images half">
Choisir une image
<img src="" alt="">
<input type="hidden" name="slide[img][]">
</div>
</a>
<div class="text half">
<label><strong>Titre : </strong></label>
<input type="text" name="slide[title][]" value="" />
<label><strong>Slogan : </strong></label>
<input type="text" name="slide[slogan][]" value="" />
<strong>Situation : </strong>
<select id="slide_situation" name="slide[text_position][]">
<option value="left" selected>Gauche</option>
<option value="right">Droite</option>
</select>
</div>
<div class="add-delete">
<span class="add">+</span><span class="delete">-</span>
</div>
</li>
</ul>
<?php
}
//Define the insides of the metabox
function save_metabox(){
submit_button();
}
Here are the contents of my class GeneratePageAdmin :
class GeneratePageAdmin
{
var $hook;
var $title;
var $menu;
var $permissions;
var $slug;
var $page;
/**
* Constructor class for the Simple Admin Metabox
*#param $hook - (string) parent page hook
*#param $title - (string) the browser window title of the page
*#param $menu - (string) the page title as it appears in the menuk
*#param $permissions - (string) the capability a user requires to see the page
*#param $slug - (string) a slug identifier for this page
*#param $body_content_cb - (callback) (optional) a callback that prints to the page, above the metaboxes. See the tutorial for more details.
*/
function __construct($hook, $title, $menu, $permissions, $slug, $body_content_cb='__return_true'){
$this->hook = $hook;
$this->title = $title;
$this->menu = $menu;
$this->permissions = $permissions;
$this->slug = $slug;
$this->body_content_cb = $body_content_cb;
/* Add the page */
add_action('admin_menu', array($this,'add_page'));
}
/**
* Adds the custom page.
* Adds callbacks to the load-* and admin_footer-* hooks
*/
function add_page(){
/* Add the page */
$this->page = add_submenu_page($this->hook,$this->title, $this->menu, $this->permissions,$this->slug, array($this,'render_page'),10);
//var_dump($this->page);
/* Add callbacks for this screen only */
add_action('load-'.$this->page, array($this,'page_actions'),9);
add_action('admin_footer-'.$this->page,array($this,'footer_scripts'));
}
/**
* Prints the jQuery script to initiliase the metaboxes
* Called on admin_footer-*
*/
function footer_scripts(){
?>
<script> postboxes.add_postbox_toggles(pagenow);</script>
<?php
}
/*
* Actions to be taken prior to page loading. This is after headers have been set.
* call on load-$hook
* This calls the add_meta_boxes hooks, adds screen options and enqueues the postbox.js script.
*/
function page_actions(){
do_action('add_meta_boxes_'.$this->page, null);
do_action('add_meta_boxes', $this->page, null);
/* User can choose between 1 or 2 columns (default 2) */
add_screen_option('layout_columns', array('max' => 2, 'default' => 2) );
/* Enqueue WordPress' script for handling the metaboxes */
wp_enqueue_script('postbox');
}
/**
* Renders the page
*/
function render_page(){
?>
<div class="wrap" id="<?php echo $this->slug; ?>">
<?php screen_icon(); ?>
<h2> <?php echo esc_html($this->title);?> </h2>
<form method="post">
<?php settings_fields( $this->slug.'-settings' ); ?>
<?php do_settings_sections( $this->slug.'-settings' ); ?>
<input type="hidden" name="action" value="some-action">
<?php wp_nonce_field( 'some-action-nonce' );
/* Used to save closed metaboxes and their order */
wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-<?php echo 1 == get_current_screen()->get_columns() ? '1' : '2'; ?>">
<div id="post-body-content">
<?php call_user_func($this->body_content_cb); ?>
</div>
<div id="postbox-container-1" class="postbox-container">
<?php do_meta_boxes('','side',null); ?>
</div>
<div id="postbox-container-2" class="postbox-container">
<?php do_meta_boxes('','normal',null); ?>
<?php do_meta_boxes('','advanced',null); ?>
</div>
</div> <!-- #post-body -->
</div> <!-- #poststuff -->
</form>
</div><!-- .wrap -->
<?php
}
}
Sorry for the long code , but I really do not know where the problem is.
Thanks you for your help ;-)
I'd say the problem lies within the way you're instantiating your class.
Use a hook to instantiate it like:
add_action( 'after_setup_theme', array( 'GeneratePageAdmin', 'init' ) );
Create the new method init of course, and do your work there.
If you're creating an options page, then the hook is likely an admin one, and not after_setup_theme, but you should get the idea.
What I would do is something like this:
public static function get_instance() {
// create an object
NULL === self::$instance and self::$instance = new self;
// return the object
return self::$instance;
}
Then, to instantiate it via Wordpress hook:
add_action( 'after_setup_theme', array( GeneratePageAdmin::get_instance(), 'init' ) );
Then, you should be able to use $this in your class
Finally, I've just had to add "action=options.php" in the header of the form and remove this section automatically handled by the settings_fields function.
<input type="hidden" name="action" value="some-action">
<?php wp_nonce_field( 'some-action-nonce' ); ?>
I have got the following code in a javascript(jquery) file called custom.js:
blabla = new Date(2014, 06 - 1, 2);
$('.days').countdown({
until: blabla,
layout: '{dn} {dl}',
1.Now i want a user to be able to change the above date. I have created a theme options page called theme-options.php
2.I am using <?php require_once('theme-options.php'); ?> in the functions.php to link to theme-options.php.
3.This is theme-options.php:
<?php
add_action('admin_menu', 'director_create_menu');
function director_create_menu() {
add_submenu_page( 'themes.php', ' Theme Options',
'Theme Options', 'administrator', __FILE__,
'director_settings_page');
add_action( 'admin_init', 'director_register_settings' );
}
function director_register_settings() {
register_setting( 'director-settings-group', 'director_date' );
}
div class="wrap">
<h2>Theme Settings</h2>
<form id="landingOptions" method="post" action="options.php">
<?php settings_fields( 'director-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Date:</th>
<td>
<input type="text" placeholder="2014, 06 - 1, 2" name="director_date"
value="<?php print get_option('director_date');
?>" />
</td>
</tr>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php } ?>
Basically what is happening is that there is a theme options page.A user gives a date inside it. Now i want to use that date inside the javascript file.
If I had to use it inside index.php it would've been
<?php $date = get_option('director_date'); ?>
<?php if( $date ) : ?> <?php echo $date; ?><?php endif; ?>);
. However this is javascript. How do i implement such an action here?
An alternative path but still doesn't work.Rename custom.js to custom.php and add:
gapinvite = new Date(<?php $date = get_option('director_date'); ?>
<?php if( $date ) : ?> <?php echo $date; ?><?php endif; ?>);
$('.days').countdown({
until: gapinvite,
layout: '{dn} {dl}',
And in functions.php:
<?php require_once('js/custom.php'); ?>
Take a look at wp_localize_script. Although ostensibly for translation, it lets you pass PHP values to JS. Here's a fairly good walkthrough on how to use it.
EDIT:
First, ensure that you are loading your JS using wp_enqueue_script, as you will need to refer to the handle. In your functions.php you'd have:
$date = get_option('director_date');
wp_enqueue_script('my-script', get_stylesheet_directory_uri() . 'js/my-script.js');
wp_localize_script('my-script', 'my_script_vars', array(
'date' => $date
)
);
wp_localize_script takes three arguments:
The handle of he script to pass the variables object to. This should match the handle being used in wp_enqueue_script
The name of the variable object you want to create. You will use this name to refer to the object in JS
The array of variable to pass to the object. Here I'm just passing the $date variable we declared earlier, but you can obviously pass whatever you want.
Then in my-script.js, you'd access this object very simply:
var $date = my_scripts_vars.date;
alert($date); // Or do whatever you want with it
In index.php, you can do something like:
<?php $date = get_option('director_date'); ?>
<script type="text/javascript">var generated_date = '<?php echo $date; ?>';</script>
Just make sure you do this before you include the JavaScript where you want to reference the generated_date variable you just created.
I have a wordpress dynamic sidebar.
<h2 style="text-align:center; ">
<?php //echo dynamic_sidebar('callus'); ?>
<?php echo dynamic_sidebar('callus'); ?>
</h2>
and the function is displaying everything ok but displaying a "1" at the bottom of the sidebar text. I searched a lot but couldnot debug. What might be the problem?
If you've got a problem with a Wordpress function, always check it in codex first (Hint: In Google enter codex followed by a space and paste the wordpress function name in there, this normally brings up the right page):
Usage
<?php dynamic_sidebar( $index ); ?>
Return Value (boolean)
True, if widget sidebar was found and called. False if not found or not called.
As you can see, do not echo. The function itself already takes care of the output. If the function now returns, let's say true, echo will additionally output 1 (boolean true to string conversion, echo is string context).
<h2 style="text-align:center; ">
<?php echo dynamic_sidebar('callus'); ?>
^^^^
</h2>
This echo is not needed. Instead:
<h2 style="text-align:center; ">
<?php dynamic_sidebar('callus'); ?>
</h2>
Here is my code for a wordpress plugin that I am developing.
<?php
/*
Plugin Name: My Javascript Plugin
*/
// add the admin options page
add_action('admin_menu', 'plugin_admin_add_page');
function plugin_admin_add_page() {
add_menu_page('Jesus Is Lord', 'Lord Jesus', 'manage_options', 'plugin',
'plugin_options_page');
}
?>
<?php
// display the admin options page
function plugin_options_page() {
?>
<div>
<h2>Jesus Is Lord</h2>
In the beginning God created the Heavens and the Earth.
<form action="options.php" method="post">
<?php settings_fields('plugin_options'); ?>
<?php do_settings_sections('plugin'); ?>
<input name="Submit" type="submit" value="<?php esc_attr_e('Jesus Saves'); ?>" />
</form>
</div>
<?php } ?>
<?php
add_action('admin_init', 'plugin_admin_init');
function plugin_admin_init(){
register_setting( 'plugin_options', 'plugin_options', 'plugin_options_validate' );
add_settings_section('plugin_main', 'Main Settings', 'plugin_section_text',
'plugin');
add_settings_field('plugin_text_string', 'Plugin Text Input',
'plugin_setting_string', 'plugin', 'plugin_main');
?>
I am new to plugin development and I can't figure out why I am getting this error:
Parse error: syntax error, unexpected $end in C:\xampp\htdocs\...\wp-content\plugins\myjavascriptplugin\myjavascriptplugin.php on line 42
You never close the function plugin_admin_init.
You're missing a closing brace } before the final ?> in your code.
YOu are missing a } in your code
I'm trying to display the html inside of a php file i have. I'm using .load('file.php') to grab it and show it in a hidden div appended via jquery. But the problem is, it wont display anything past a php line in the file. Like this:
<div id="content">
<h1> Welcome </h1>
<form action="<?php activity_post_form_action()?> "method="post" id="whats-new-form" name="whats-new-form">
Everything below the form wont show, but the <h1> Welcome </h1> will show.
All of the html and such is valid, meaning I closed all of my tags and such. It just halts after it sees an php line. I removed the php from the form, and more showed up until the next php line.
Its definitely something i did wrong...lol, first rule to coding: EVERYTHING is your fault :)
Any ideas what i did wrong?
also, if you want a look at my jquery line :
jQuery('#window').load('file.php');
This is the content of the file.php:
<div id="content">
<div class="raberu"><h6>post update</h6></div>
<h1> Welcome </h1>
<form action="<?php bp_activity_post_form_action()?>" method="post" id="whats-new-form" name="whats-new-form">
<?php do_action( 'bp_before_activity_post_form' )?>
<div class="content-body">
<div class="top-content">
<h5>
<?php if ( bp_is_group() ) : ?>
<?php printf( __( "What's new in %s, %s?", 'buddypress' ), bp_get_group_name(), bp_get_user_firstname() ) ?>
<?php else : ?>
<?php printf( __( "So, what's up %s?", 'buddypress' ), bp_get_user_firstname() ) ?>
<?php endif; ?>
</h5>
</div>
<div class="avatar"><img src="<?php bp_loggedin_user_avatar('html=false');?>"/></div>
<div class="foremu">
<div id="whats-new-textarea">
<span class="arato">Click here to start typing</span>
<textarea name="whats-new" id="whats-new">
</textarea>
</div>
<div id="whats-new-options">
<div id="whats-new-submit">
<span class="ajax-loader"></span>
<input type="submit" name="aw-whats-new-submit" id="aw-whats-new-submit" value="<?php _e( 'Post Update', 'buddypress' )?>"/>
<input type="button" class="cancel" value="cancel"/>
</div>
</div>
</div>
And when I check my error counsel, I get This : PHP Fatal error: Call to undefined function and it says this for each line of php code that works just fine if I load that page as a template file via wordpress/php...
EDIT**
Ok, I found this, I just have to figure out how to work with it:
When you post your ajax call from javascript using jQuery, you can define the action
which will determin which function to run in your PHP component code.
*
Here's an example:
*
In Javascript we can post an action with some parameters via jQuery:
jQuery.post( ajaxurl, {
action: 'my_example_action',
'cookie': encodeURIComponent(document.cookie),
'parameter_1': 'some_value'
}, function(response) { ... } );
*
Notice the action 'my_example_action', this is the part that will hook into the wp_ajax action.
You will need to add an add_action( 'wp_ajax_my_example_action', 'the_function_to_run' ); so that
your function will run when this action is fired.
You'll be able to access any of the parameters passed using the $_POST variable.
So if anyone is having trouble implementing php functions via ajax in wordpress, buddypress etc. Maybe this will help :)
<form action="<?php activity_post_form_action()?> "method="post" id="whats-new-form" name="whats-new-form">
You have a space after your closing PHP tag. so your action might be showing up like this:
<form action="whatever.php ">
However, I don't think that is your issue.
Your jQuery syntax looks correct. (http://api.jquery.com/load/)
An easy way to double check for Javascript errors you can use Firebug for Firefox (https://addons.mozilla.org/en-US/firefox/addon/1843/)
It could have to do with running on a local server. Do you know the details of your server setup?
EDIT:
If call to undefined function is an error you are getting, it is likely that the standalone PHP file is failing to call functions that are included in the template.
When you include that PHP file on a template page it works fine. HOWEVER, if you call a PHP file via AJAX, it cannot access functions on the page you loaded from.
ajaxphpfile.php
whatever();
Example1.php
function whatever() {
echo "hello!";
}
include('ajaxphp.file.php');
Example2.php
function whatever() {
echo "hello!";
}
jQuery('#window').load('ajaxphpfile.php');
Example2.php will throw an error when loaded using .load() because it can't access the whatever() function. You are trying to call wordpress functions in a standalone php file when you have not included them. Does that make sense?