Data not saved Wordpress Custom Admin Page - php

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

Related

Woocommerce add Account and Basket in menu

Having recently worked on a project requiring an icon for the connections page and also the cart,
I haven't found much on this problem.
Here is how to activate it.
First, go to woocommerce.php and look for this line.
/**
* Sample implementation of the WooCommerce Mini Cart.
*
* You can add the WooCommerce Mini Cart to header.php like so ...
*
*<?php
* if ( function_exists( 'godault_metal_woocommerce_header_cart' ) ) {
* godault_metal_woocommerce_header_cart();
* }
*?>
*/
Then you must uncomment it and your cart will be activated.
To display it, just add your corresponding function in your header.php
<div class="panier">
<?php
if ( function_exists( 'godault_metal_woocommerce_header_cart' ) ) {
godault_metal_woocommerce_header_cart();
}
?>
</div><!--#cart -->
Then finally to activate put a link to the user account do the same.
Go to woocommerce.php add this by changing the name of the function.
if (! function_exists('godault_metal_woocommerce_header_account')) {
/**
* Display Header Account.
*
* #return void
*/
function godault_metal_woocommerce_header_account() {
?>
<div class="header-my-account">
<div class="header-login">
<a href="<?php echo esc_url(get_permalink(get_option('woocommerce_myaccount_page_id'))); ?>" title="Account">
<i class="fas fa-user"></i>
</a>
</div>
</div>
<?php
}
}
Add it in the header.php.
<div class="account">
<?php
if ( function_exists('godault_metal_woocommerce_header_account')) {
godault_metal_woocommerce_header_account();
}
?>
</div><!--#account -->

Create a option page for this simple plugin

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/

WordPress widget outputs a unwanted number

I made a WordPress widget that is only supposed to display an image but is displaying a number also. I don't know why this happens.
Widget output code:
public function widget( $args, $instance ) {
?>
<img src="<?= $instance['image'] ?>" alt="" class="img-responsive">
<?php
}
Widget area it's in:
<div class="spotlight">
<?= dynamic_sidebar('header') ?>
</div>
Unwanted output (the image is correct but it also prints a "1"):
Any help on how to fix this?
Change your code to:
<div class="spotlight">
<?php dynamic_sidebar('header'); ?>
</div>
The <?= short open tag means <?php echo and dynamic_sidebar function returns true (which is converted to number in your case) if widget sidebars exists.

How to call data array to home.ctp cakephp3

I am new to cakephp. I am trying to build home page with carousel.But I want to make the image in the slide get called from the slides table database.
Below is my carousel code in home.ctp
<div class="mbr-box mbr-section mbr-section--relative mbr-section--fixed-size mbr-section--bg-adapted item dark center mbr-section--full-height active" style="background-image: url(<?= ($slide->thumbnail) ?>);">
<div class="mbr-box__magnet mbr-box__magnet--sm-padding">
<div class=" container">
<div class="row"><div class="col-sm-8 col-sm-offset-2">
<div class="mbr-hero">
<h1 class="mbr-hero__text">Slide 2</h1>
<p class="mbr-hero__subtext">Description</p>
</div>
<div class="mbr-buttons btn-inverse mbr-buttons--center"><a class="mbr-buttons__btn btn btn-lg btn-danger" href="#">HERE</a></div>
</div></div>
</div>
</div>
</div>
style="background-image: url(<?= ($slide->thumbnail) ?>);" is the code that i thought could call the image file from database(images/17860481920x1200-165.jpg). But all i got is a blank.
Can anyone help me. Sorry for my bad english.
Easy way:
cake bake cell Carousel
this create two files,
first CarouselCell.php in View/Cell
<?php
namespace App\View\Cell;
use Cake\View\Cell;
/**
* Carousel cell
*/
class CarouselCell extends Cell
{
/**
* List of valid options that can be passed into this
* cell's constructor.
*
* #var array
*/
protected $_validCellOptions = [];
/**
* Default display method.
*
* #return void
*/
public function display()
{
}
}
secound display.ctp in Template\Cell\Carousel folder.
In CarouselCell.php display method add:
public function display()
{
$this->loadModel('Slides');
$slides = $this->Slides->find('all');
$this->set('slides', $slides->toArray());
}
in display.ctp
<?php foreach($slides as $slide) { ?>
YOUR CAROUSEL HTML HERE
<?php } ?>
On home.ctp
<?= $this->cell('Carousel');?>
Read more: http://book.cakephp.org/3.0/en/views/cells.html
// First you pass data from the controller:
public function home() {
$slider = array('img1.jpg','img2.jpg');
$this->set('slider', $slider);
}
app/View/Pages/home.ctp
<?php print_r ($slider); ?>

Using php inside javascript in wordpress

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.

Categories