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
Related
I want to create an option page to edit the $date variable, and change the image src of this simple plugin, please help a php noob =)
<?php
/*
Plugin Name: Masthead for xxx
Description: Masthead for xxx
Author: Red Mariachi
Version: 0.2 [26/01/2019]
*/
add_action( 'avada_before_header_wrapper', 'avada_add_banner' );
function avada_add_banner() {
$date='24/01/19';
if(\DateTime::createFromFormat('d/m/y',$date) > new \DateTime()){
//data passata
echo '<img class="size-full wp-image-40510 aligncenter" src="http://xxx.it/xxx/wp-content/uploads/2019/01/backup-1.jpg" alt="" width="100%" height="356" />';
} else{
//data è odierna o futura
echo '<h1 style="text-align:center; font-size:40px;">BANNER SCADUTO</h1>';
}
}
/**
* Register a custom menu page.
*/
function my_custom_menu_page(){
add_menu_page(
__( 'Custom Menu Title', 'textdomain' ),
'custom menu',
'manage_options',
'custompage',
'my_custom_menu_page_html',
plugins_url( 'myplugin/images/icon.png' ),
6
);
}
add_action( 'admin_menu', 'my_custom_menu_page' );
/**
* Display a custom menu page
*/
function my_custom_menu_page_html(){
?>
<form method="POST" action="admin-post.php">
<input type="hidden" name="action" value="update_my_date_action">
<input type="date" name="my_date">
<input type="submit" value="Submit">
<?php
}
function update_my_date_action(){
update_option( 'my_custom_date', $_POST['my_date']);
}
add_action( 'admin_post_update_my_date_action', 'update_my_date_action' );
[process your POST data, for your needs]
You have to check the documentation for more details.
https://developer.wordpress.org/reference/functions/add_menu_page/
https://codex.wordpress.org/Function_Reference/update_option
https://developer.wordpress.org/reference/functions/get_option/
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().
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 am developing a theme setting page for my client. I want to add a theme options page to apply settings but I get Wordpress error "You do not have sufficient permissions to access this page. I think I am using the right code but problem still exists.
Please help me out in figuring where I am wrong
/*
* ADD THEME SETTINGS PAGE
*/
function vc_add_theme_settings_page(){
add_theme_page('Theme Settings','Theme Settings', 'manage_options' , 'vc_theme_page', 'vc_theme_page_display' );
}
add_action('admin_init', 'vc_add_theme_settings_page');
/*
* DISPLAY THEME SETTINGS PAGE
*/
function vc_theme_page_display(){
?>
<div class="wrap">
<h2>Vc Theme Settings Page</h2>
<form action="options.php" method="POST">
<?php settings_fields('vc_section'); ?>
<?php do_settings_sections('vc_theme_page'); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
Order your code at this way:
add_action('admin_menu', 'vc_add_theme_settings_pages');
add_action('admin_head', 'theme_styles');
add_action('admin_init', 'vc_add_theme_settings_page');
function vc_add_theme_settings_page(){
add_theme_page('Theme Settings','Theme Settings', 'edit_theme_options', 'manage_options', 'vc_theme_page_display' );
}
and try change
<?php do_settings_sections('vc_theme_page'); ?>
for
<?php do_settings_sections(__FILE__); ?>
add_action('admin_init', 'vc_add_theme_settings_page'); //instead of this
add_action('admin_menu', 'vc_add_theme_settings_page'); //try this
Hope it will work for you.
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.