when we want to do add_action inside a class we usually do like this:
class Myclassprefix_Some_Class extends Some_Class {
function __construct(){
add_action('wp_head', array($this, 'myfuncprefix_add_meta_tag'));
}
function myfuncprefix_add_meta_tag(){
echo '<meta name="description" content="This is an example meta tag" />';
}
}
Now I want to call the parent method instead.
I want to change:
add_action('wp_head', array($this, 'myfuncprefix_add_meta_tag'));
to:
add_action('wp_head', array($parent, 'some_parent_method'));
How to change $parent correctly? I know I can do like this:
add_action('wp_head', array($this, 'some_parent_method'));
function some_parent_method(){
parent::some_parent_method();
}
Is there any other way to do this without writing the function to call parent function?
You can call parent class method without writing a new function to do that. The only requirement is that parent class method is defined as public.
class ParentClass {
public function someMethod() {
print('Hi');
}
}
class ChildClass extends ParentClass {
public function __construct() {
add_action('wp_head', [ $this, 'someMethod']);
}
}
Related
I'm using Timber plugin, and I would like to have separate files for registering CPTs, taxonomies etc. I have a lot of those and do not want to clutter up the functions.php file. I know I can just use require_once(), but I'm not sure if it is the proper way.
class StarterSite extends Timber\Site {
public function __construct() {
...
add_action( 'init', array( $this, 'register_post_types' ) );
...
parent::__construct();
}
public function register_post_types() {
// Proper way to have this data in a different file?
}
}
I thought this would work without question but I am missing something.
In my wordpress child functions.php:
function HelloWorldShortcode() {
return My_Custom_Plugin_Public::display_custom_block();
}
add_shortcode('helloworld', 'HelloWorldShortcode');
display_custom_block() function:
public static function display_custom_block() {
echo "hello world hello world";
}
Unfortunately the page just cannot load this shortcode. Am I not able to call a class function from within a shortcode?
Whole class:
<?php
defined( 'ABSPATH' ) or die();
class My_Custom_Plugin_Public {
private $plugin_name;
private $version;
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
}
public static function display_custom_block() {
echo "hello world hello world";
}
}
First of all import the class file to functions.php as you said it's custom
require_once( __DIR__ . '/YourCustomClass.php'); //Path of file
Then in your function you can call like this
function HelloWorldShortcode() {
return My_Custom_Plugin_Public::display_custom_block();
}
add_shortcode('helloworld', 'HelloWorldShortcode');
This should work
I am new to sage theme. I am trying to add custom widget in lib/setup.php file, but i get Class 'Roots\Sage\Setup\WP_Widget' not found in {path} error.
Following is my code :
class Banner_Widget extends WP_Widget {
function __construct() {
$this->WP_Widget('Banner-Widget', __('Banner Widget', 'blogerist'), $widget_ops);
add_action('save_post', array(&$this, 'flush_widget_cache'));
add_action('deleted_post', array(&$this, 'flush_widget_cache'));
add_action('switch_theme', array(&$this, 'flush_widget_cache'));
}
public function form($instance) {
//form content
}
function widget($args, $instance) {
//widget content
}
function update($new_instance, $old_instance) {
$instance = array_map('strip_tags', $new_instance);
$this->flush_widget_cache();
$alloptions = wp_cache_get('alloptions', 'options');
if (isset($alloptions['Banner-Widget'])) {
delete_option('Banner-Widget');
}
return $instance;
}
function flush_widget_cache() {
wp_cache_delete('Banner-Widget', 'widget');
}
}
When you want to create your class by extending the WP_Widget then you should use the global namespace.
Add a \ sign before WP_Customize_Control.
class Banner_Widget extends \WP_Widget {
//....
}
I am talking about making a widget which extends a child widget of WP_Widget.
Using this as reference: https://wordpress.stackexchange.com/questions/101438/how-to-extend-a-wp-widget-twice
Example:
My_WidgetBase extends WP_Widget
My_Widget extends My_WidgetBase
I want to know how to get My_Widget to show up with my other widgets (it is not currently). I have gotten My_WidgetBase to work.
This is important for my framework. I understand that widget(), update(), and form() must be overridden, but I could not find anything about making a grandchild of WP_Widget anywhere.
Example:
class My_WidgetBase extends WP_Widget {
public function __construct($id = 'my-widget-base', $desc = 'My WidgetBase', $opts = array()) {
$widget_ops = array();
parent::__construct( $id, $desc, $widget_ops );
}
public function widget( $args, $instance ) {
die('function WP_Widget::widget() must be over-ridden in a sub-class.');
}
public function update( $new_instance, $old_instance ) {
return $new_instance;
}
public function form( $instance ) {
echo '<p class="no-options-widget">' . __('There are no options for this widget.') . '</p>';
return 'noform';
}
}
function RegisterMyWidgets() {
register_widget('My_WidgetBase');
}
add_action( 'widgets_init', 'RegisterMyWidgets' );
Solved digging further into this thread: https://wordpress.stackexchange.com/questions/101438/how-to-extend-a-wp-widget-twice
The problem was not with my implementation of the classes, but how I included the parent in my framework. I was not working with both child and grandchild in the same php file, but two separate files with different directories.
Solving this problem was a matter of using require_once() correctly in the directory of my grandchild class.
I am very new to Wordpress and I need some help with this one.
I have this widget:
class MyWidget extends WP_Widget {
...
function widget($args, $instance) {
...
}
}
Then outside the widget class I have a javascript to which I have to pass the widget parameters:
function MyWidget_load_scripts() {
wp_enqueue_script(
'my-scripts', plugins_url('assets/js/myscript.js', __FILE__), array('jquery')
);
wp_localize_script('my-scripts', 'params', $widgetParameters);
}
add_action('wp_enqueue_scripts', 'MyWidget_load_scripts');
How can I get the widget parameters?
Thanks in advance