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/
Related
I searched on why my get_header() function is not working in my plugin. All possible solutions say that the index.php file might be corrupt. But in my case it's working fine. My error is different from other errors solved on stackexchange concerning get_header().
Please also help me on how to show my searchresult.php from plugin folder. If I add any wordpress function in it, it says undefined function. like get_header(), plugin folder etc.
My code:
<?php
get_header();
// Start the loop.
global $wpdb;
$srh = $_POST['s'];
//get_search_query();
if(!empty($srh)) {
echo $srh;
// Search title from table
$sql = "SELECT * FROM wp_article WHERE title LIKE '$srh'";
$result = $wpdb->get_results($sql) or die(mysql_error());
if(count($result) > 0) {
foreach($result as $results) {
echo $results->title;
echo $results->desc;
}
} else {
echo "no ";
}
} // submit end
?>
searchform.php
<style>
.search{
border:0px solid #E5E5E5;
width: 100%;
height:90px;
padding:0px;
}
.search ul li{
list-style:none;
padding:0px;
display:inline-block !important;
position:relative;
float:none !important;
text-align:left;
}
.widget input.search-field[type="search"] {
width:100%;
}
</style>
<div class="search"><?php
/**
* Template for displaying search forms in Twenty Sixteen
*
* #package WordPress
* #subpackage Twenty_Sixteen
* #since Twenty Sixteen 1.0
*/
$url = plugins_url();
get_header();
?>
<form role="search" method="get" class="search-form" action="<?php echo $url; ?>/magicsearch/searchm.php" >
<label>
<span class="screen-reader-text"><?php echo _x( 'Search for:', 'label', 'twentysixteen' ); ?></span>
<input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder', 'twentysixteen' ); ?>" value="<?php echo get_search_query(); ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label', 'twentysixteen' ); ?>" />
</label>
<button type="submit" class="search-submit"><span class="screen-reader-text"><?php echo _x( 'Search', 'submit button', 'twentysixteen' ); ?></span></button>
</form>
</div>
**My Plugin Dashboard file **
plugin_index.php
<?php
/*
Plugin Name: Magic Search
Plugin URI: #
Description: Magic Search use for to find article from archive otherwise query saved.
Author: Manmohan
Author URI: #
*/
// PLUGIN ACTIVATION BY CODE
//function myplugin_activate() {
// Activation code here...
//}
//register_activation_hook( __FILE__, 'myplugin_activate' );
function createTable(){
global $wpdb;
$query = "CREATE TABLE IF NOT EXISTS " . $wpdb->prefix ."newkeyword (
query_id INT NOT NULL AUTO_INCREMENT,
keyword VARCHAR(25) NOT NULL,
PRIMARY KEY (query_id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$wpdb->query($query);
}
register_activation_hook(__FILE__, 'createTable');
//ADD SHORTCODE
register_activation_hook(__FILE__, 'searchbar_function');
add_shortcode('searchbar', 'searchbar_function');
function searchbar_function(){
include('searchf.php');
include('searchm.php');
}
// Hook for adding admin menus
add_action('admin_menu', 'disp_queries');
// action function for above hook
function disp_queries(){
/* Browser Top Title use at */
/* Title Text WP Admin LFT Bar */
/* \/ \/ */
add_menu_page( 'Magic Search', 'Magic Search', 'manage_options', 'searchpage', 'my_custom_menu_page', plugins_url( 'magicsearch/se.png' ), 6 );
}
function my_custom_menu_page(){
include('adminresult.php');
//tguy_sm_summary_table(30);
}
?>
have you set up the header and footer .php files in the theme folder?
may seem like a silly question, but its been known to happen.
regarding the search question, im not 100% sure on that one, but this get_search_form topic may help
Is this a custom theme or one you have picked as default?
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 gallery plugin. So i created a custom post type and under it i created a submenu page for changing gallery options such a border-width, border-color, border-radius etc. when i update the values, all are working but update notification is not showing.
I am writing main code here. Please let me know what is problem.
function gallery_setting_pages() {
add_submenu_page(
'edit.php?post_type=gallary',
'Gallery Settings',
'Settings',
'manage_options',
'gallery-setting',
'gallery_option_page_functions'
);
}
add_action('admin_menu','gallery_setting_pages');
if ( is_admin() ) :
function ppmscrollbar_register_settings() {
register_setting('gallery_plugin_options','rt_gallery_options','rt_gallery_validate_options');
}
add_action('admin_init','ppmscrollbar_register_settings');
function gallery_option_page_functions() {
global $rt_gallery_options;
if ( ! isset ($_REQUEST['updated'] ) )
$_REQUEST['updated']=false;
?>
<?php if ( false !==$_REQUEST['updated'] ) : ?>
<div class="updated fade"><p><strong><?php _e('options saved'); ?></strong></p></div>
<?php endif; // If the form has just been submitted, this show the notification ?>
<form method="post" action="options.php">
<?php $settings=get_option( 'rt_gallery_options', $rt_gallery_options); ?>
<?php settings_fields('gallery_plugin_options'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="cursor_color">Select Gallery Border Color</label></th>
<td>
<input id="cursor_color" type="text" name="rt_gallery_options[cursor_color]" value="<?php echo stripslashes($settings['cursor_color']); ?>" class="my-color-field" /><p class="description">In a few words, explain what is this.</p>
</td>
</tr>
</table>
<p class="submit"><input type="submit" value="Save Changes" class="button button-primary" id="submit" name="submit"></p>
</form>
</div>
<?php
}
function rt_gallery_validate_options( $input ){
global $rt_gallery_options;
$settings = get_option( 'rt_gallery_options', $rt_gallery_options );
//We strip all tags from the text field, to avoid vulnerablilties like XSS
$input['cursor_color']=wp_filter_post_kses( $input['cursor_color']);
return $input;
}
endif; //EndIf is_admin()
thanks for being posted the question on stackoverflow to get answers. We are here to help you.Please mention the page name you are using above , and let me know where you are updating the variable "updated" in your page. The issue might be, if you are not setting the variable, then if condition always fails, and notification will not be shown.
I am trying to add color picker in wordpress settings API for plugin development. But I am facing problem to do that. I have code this for color picker.
// Create this function for color picker.
add_action( 'admin_enqueue_scripts', 'mw_enqueue_color_picker' );
function mw_enqueue_color_picker( $hook_suffix ) {
// first check that $hook_suffix is appropriate for your admin page
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'my-script-handle', plugins_url('my-script.js', __FILE__ ), array( 'wp-color-picker' ), false, true );
}
//Call it in my input field option
<tr valign="top">
<th scope="row"><label for="cursor_color">ScrollBar Color</label></th>
<td>
<input id= "cursor_color" type="text" name="ppmscrollbar_options[cursor_color]" value="<?php echo stripslashes($settings['cursor_color']);?>" class="my-color-field"/><p class="description">Select Icon holder color here. You can also add html HEX code.</p>
</td>
</tr>
//In my-script.js file I have written this bellow code.
jQuery(document).ready(function($){
$('.my-color-field').wpColorPicker();
});
I did not solve the issue. Can anyone tell me what can I do?
I'm really not sure why your code is not working, maybe the issue is not in the code you posted... The following is almost the same as yours, but a complete demonstration:
add_action('admin_menu', 'color_pick_so_23696173');
function color_pick_so_23696173()
{
$my_page = add_dashboard_page(
'colorpick',
'colorpick',
'add_users',
'colorpick-page',
'color_pick_callback_so_23696173'
);
add_action( "admin_print_scripts-$my_page", 'enqueue_so_23696173' );
}
function enqueue_so_23696173()
{
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script(
'colorpick',
plugins_url( 'my-script.js', __FILE__ ),
array( 'wp-color-picker'),
false,
true
);
}
function color_pick_callback_so_23696173()
{
?>
<div class="wrap">
<h2>Test</h2>
<table>
<tr valign="top">
<th scope="row"><label for="cursor_color">ScrollBar Color</label></th>
<td>
<input id= "cursor_color" type="text" name="ppmscrollbar_options[cursor_color]" value="" class="my-color-field"/>
<p class="description">Select Icon holder color here. You can also add html HEX code.</p>
</td>
</tr>
</table>
</div>
<?php
}
And my-script.js is exactly the same as yours.
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