Calling a class function from functions.php shortcode - wordpress

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

Related

how to add_action callback for parent method?

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']);
}
}

Wordpress hook that will instantiate a class to be able to use get_post_types before publish_CPT hook

I have an issue where I need to instantiate a class in wordpress so that in the constructor I can use the function get_post_types and have that hook happen before the publish_post hook (which I assume is around the publish_CPT hooks).
Here is the code I have so far
class Transient_Delete {
/**
* #var array of all the different post types on the site
*/
private $postTypes;
/**
* #var array of wordpress hooks we will have to assemble to delete all possible transients
*/
private $wpHooks;
public static function init() {
$class = __CLASS__;
new $class;
}
public function __construct()
{
$this->postTypes = array_values( get_post_types(array(), 'names', 'and') );
$this->wpHooks = $this->setWpHooks($this->postTypes);
add_action('publish_alert', array($this, 'deleteAlertTest'));
}
private function setWpHooks($postTypes)
{
$hooks = array_map(function($postType) {
return 'publish_' . $postType;
}, $postTypes);
return $hooks;
}
private function deleteAlertTest($post)
{
$postId = $post->ID;
echo 'test';
}
}
add_action( 'wp_loaded', array( 'Transient_Delete', 'init' ));
Another note here is that this is in the mu-plugins directory.
note: "alert" of publish_alert is a custom post type.
Ok this was my fault, it looks like the hook publish_alert works fine if I change the deleteAlertTest function to public. Any idea on why having it be a private function has that effect? Its within the same class.

Error while adding custom widget in sage theme

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 {
//....
}

How to extend a custom widget in wordpress?

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.

wordpress plugin shortcodes buffer

My developed wordpress plugin which is activated with a shortcode is breaking my admin area saying that header cannot be modified. Digging a bit deeper I got to know that if the function is getting echoed than I have this problem if I use return than is ok. But the problem with return is: that I use ajax to retrieve html and in this case no output is generated.
message Cannot modify header information - headers already sent by (output started at /var/www.... web/wordpress/wp-admin/admin-header.php
MyClass{
public function __construct()
public $data;
{
require_once(dirname(__FILE__) . '/class/class.another.php');
$this->data = new Another();
add_action( 'init', array( &$this, 'init' ) );
}
public function init()
{
add_shortcode( 'my_shortcode', array ($this, 'shortcode') );
if(isset($_POST['id'])){
$param = $this->data->output_ajax_html($_POST['id']);
echo $this->shortcode_html_extended($param);
//this part breaks the buffer without echo is working but the contertn won't show up
}
}
public function shortcode()
{
add_shortcode( 'my_shortcode', array ($this, 'shortcode_html') );
}
public function shortcode_html()
{
$html = "";
$html .="";
return $html;
}
public function shortcode_html_extended($param)
{
$html = "";
//mixed with php
$html .="";
return $html;
}
}
$test = new MyClass();

Resources