So my plan was to add custom banner to all pages simply using code in functions.php, it works, but not how I would like to
Code:
add_action( 'woocommerce_after_main_content', 'after_main_content', 1 );
function after_main_content() {
echo ' <style>
.img-container {
text-align: center;
}
</style>
<div class="img-container">
<img src="https://www.prospecs.lt/wp-content/uploads/2020/12/dpdtt-1.png" >
</div>
';
}
Problem:
Only in pages with sidebars my banner get's allocated to another section.
Any ideas how to place it in the right place on pages with sidebar?
my site: www.prospecs.lt
I suggest, you should use a theme footer action. Which is: ocean_after_main So the code would be:
add_action( 'ocean_after_main', 'after_main_content', 1 );
function after_main_content() {
if( is_shop() ){
echo ' <style>
.img-container {
text-align: center;
}
</style>
<div class="img-container">
<img src="https://www.prospecs.lt/wp-content/uploads/2020/12/dpdtt-1.png" >
</div>
';
}
}
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'm in the process of a building a small WordPress bottom right corner popup. I ran into some trouble. This is my first time building a WordPress plugin. Anyways, I can't figure out how to show it on the front static page of my website. Please help?
This is the only file I have in my Plugin folder. Where is my
mistake?
Here's my code:
<?php
/*
Plugin Name: My First Plugin
Plugin URL: http://www.martinshabacom.ipage.com/test
Description: An awesome facebook popup plugin that will amaze you!
Author: Martin
Version: 1.0
Author URL: http://www.martinshabacom.ipage.com/test
*/
add_action('admin_menu', 'myfirstplugin_admin_actions');
function myfirstplugin_admin_actions() {
add_options_page('MyFirstPlugin', 'MyFirstPlugin', 'manage_options', __FILE__, 'myfirstplugin_admin');
}
function myfirstplugin_admin()
{
?>
<style>
.wrap {
width:100%;
height:auto;
}
.popup {
position:fixed;
bottom:0;
right:0;
width:325px;
height:200px;
background-color:#09f;
}
</style>
<div class="wrap">
<h1>Hello World!</h1><br>
<h4>Hope you like my awesome popup!</h4>
</div>
<div class="popup">
<?php if(is_front_page()) {
Hello world
}
?>
</div>
<?php
}
?>
I think you want to add output to the footer using the wp_footer action, not the admin_menu. The wp_footer is used to add output when the wp_footer() method is called by your theme. It's a useful place for floating elements on the website.
So it would be
add_action('wp_footer', 'myfirstplugin_admin_actions');
and make sure your theme has the <?php wp_footer() ?> line somewhere appropriate.
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' ); ?>
Its driving me crazy as im not code whiz, using a template 'submate' im trying to add a right sidebar on a child theme vantage
<?php
/**
* This template displays full width pages with a page title.
*
* #package vantage
* #since vantage 1.0
* #license GPL 2.0
*
* Template Name: submate
*/
get_header(); ?>
<?php
//get the sidebar
$avia_config['currently_viewing'] = 'page';
if(is_front_page()) { $avia_config['currently_viewing'] = 'frontpage'; }
get_sidebar();
?>
<div class="breadcrumbs">
<?php if(function_exists('bcn_display'))
{
bcn_display();
}?>
</div>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
<?php get_footer(); ?>
The link for it is; http://www.seasonalharvest.co.uk/recipes/february/
Many thanks for any help :)
Your html structure for article is very bad. You have wrapped your article element within strong element this doesn't make sense as it use for make the text bolder.
Anyway you can add this tiny snippet to your wp-content/themes/your-theme/style.css
article[class*="post"] { /* only post articles */
float: left;
width: 65%;
}
Edit
For specific page only:
.page-id-231 article[class*="post"] { /* only post articles */
float: left;
width: 65%;
}
Try this...
<style type="text/css">
article#post-231 {
float: left;
width: 65%;
}
</style>