Wordpress shortcode is not called - wordpress

Okay, I have created a plugin and now want to provide shortcode to app.
Here is my only file in wp-content/plugins/my-plugin/my-plugin.php
<?php
/**
* Plugin Name: Latest Issue
* Author: Max Tsepkov
* Author URI: http://www.yogi.pw
*/
add_action('init', function() {
add_shortcode('my-plugin', function() {
// ... my code
return 'string';
});
});
I know that plugin is activated and the callback for init is called.
But the shortcode function is never get called.
I add text [my-plugin] to a widget, and it isn't replaced as well.
What do I do wrong? How to correctly register a shortcode?

I guess you PHP is at least 5.3, so you can make it work in a widget, you need to add this code.
add_filter('widget_text', 'do_shortcode');
I tested your code and it works.

Turned out, that some themes are not parsing shortcodes in widgets.
We can explicitly hook into theme filter and let it run shortcodes in widgets.
For details see https://wordpress.org/support/topic/how-to-make-shortcodes-work-in-a-widget
And there is no need for hook into init action. This code works:
<?php
/**
* Plugin Name: Latest Issue
* Author: Max Tsepkov
* Author URI: http://www.yogi.pw
*/
// Allow theme to parse shortcodes in widgets
add_filter('widget_text', 'do_shortcode');
add_shortcode('my-plugin', function() {
// ... my code
return 'string';
});

Related

Updating failed after adding shortcode

I have created a new plugin for youtube video slider. I have used below code for plugin creation. After activating this plugin, post and page update is not working. I am getting the error is "Updating failed"
I have used shortcode like this [az_youtube_slider] in my post.
Can you please anyone help me to fix the error.
Thanks in advance.
<?php
/*
Plugin Name: Assistanz youtube slider
Plugin URI: https://www.assistanz.com/
Description: Get videos from youtube assistanz channel and slider
Version: 1.0
Author: Safia
Author URI: https://assistanz.com/
License: GPLv2 or later
Text Domain: assistanz
*/
if(!defined('ABSPATH')) exit;
function az_youtube_slider($atts, $content = null) {
return "<p>Youtube video will come here</p>";
}
add_shortcode("az_youtube_slider", "az_youtube_slider");
?>
Use 'Classic Editor' plugin to fix your issue also your function will not work, please update your function with below.
function displayDate($atts, $content = null) {
return "<p>Youtube video will come here</p>";
}
add_shortcode("az_youtube_slider", "displayDate");

how to remove wordpress documentation menu link in dashboard

I want to customize my dashboard by removing WordPress icon and it's menu on the top bar but I have no idea of how it works because am not an expert in WordPress please help me
Create a new file in the WordPress wp-content/plugins/ folder named admin-bar.php then add the following plugin header:
<?php
/*
Plugin Name: Admin Bar
Plugin URI: http://www.sitepoint.com/
Description: Modifies the WordPress admin bar
Version: 1.0
Author: Craig Buckler
Author URI: http://twitter.com/craigbuckler
License: MIT
*/
You can now activate this plugin in the WordPress administration panel. It won’t do anything yet but you can make additions, save then refresh to view the updates.
you can remove existing items with the remove_node() method. For this, we need to create a new function named update_adminbar() which is passed an WP_Admin_Bar object ($wp_adminbar). This function is called when the admin_bar_menu action hook is activated:
// update toolbar
function update_adminbar($wp_adminbar) {
// remove unnecessary items
$wp_adminbar->remove_node('wp-logo');
$wp_adminbar->remove_node('customize');
$wp_adminbar->remove_node('comments');
}
// admin_bar_menu hook
add_action('admin_bar_menu', 'update_adminbar', 999);
https://www.sitepoint.com/customize-wordpress-toolbar/
You can create a custom plugin and upload folder to your server with this one file in it. Make sure to save the file as exact plugin name. For example, "AdminBar.php"
<?php
/*
Plugin Name: AdminBar
Plugin URI:
Description: Code to hide the admin bar for non-admins only.
Version: 1.0
Author: Name Here
Author URI:
*/
function hide_admin_bar_settings()
{
?>
<style type="text/css">
.show-admin-bar {
display: none;
}
</style>
<?php
}
function disable_admin_bar()
{
if(!current_user_can('administrator'))
{
add_filter( 'show_admin_bar', '__return_false' );
add_action( 'admin_print_scripts-profile.php', 'hide_admin_bar_settings' );
}
}
add_action('init', 'disable_admin_bar', 9);

Using Woocommerce Hooks in a Wordpress Plugin

I created a sample plugin for using woocommerce hooks. Basically my requirement was to add some javascript to the footer of the wordpress page based on some woocommerce hooks. However, those hooks don't seem to get fired at all. I have woocommerce installed. If I put the same code in theme's function file, the javsacript gets added, but not from the plugin.
In the plugin, there are three actions. The first action is a plain wp_footer action which works and js is added. the remaining two are woocommerce actions and are not getting fired. Can anyone please help? I am sure I am calling the hooks the wrong way but I can't figure out.
<?php
/*
* Plugin Name: Demo Woo Plugin
* Plugin URI:
* Description:
* Version: 1.0
* Author:
* Author URI:
* License: GPLv2
*/
/*
*/
if(!class_exists('Demowoo')) {
class Demowoo {
var $plugin_url;
var $plugin_dir;
public function __construct() {
global $woocommerce;
$this->plugin_url = trailingslashit( WP_PLUGIN_URL . '/' . dirname(plugin_basename(__FILE__)) );
$this->plugin_dir = trailingslashit( plugin_dir_path(__FILE__) );
add_action( 'wp_footer', array($this, 'demowoo_content') );
// initiate woocommerce hooks and activities
add_action('woocommerce_init', array($this, 'on_woocommerce_init'));
add_action('woocommerce_after_cart_contents', 'cart_page_visited');
}
public function install() {
}
public function deactivate() {
}
/**
* Append the required javascript.
*/
public function demowoo_content() {
echo '<script type="text/javascript">console.log("Demo Plugin Content");</script>';
}
public function on_woocommerce_init() {
add_action('wp_footer', 'woocommerce_initialized');
}
public function woocommerce_initialized() {
echo '<script type="text/javascript">console.log("JS through woo commerce init.");</script>';
}
public function cart_page_visited() {
add_action('wp_footer', 'demo_woo_add_to_cart');
}
public function demo_woo_add_to_cart() {
echo '<script type="text/javascript">console.log("JS for added_to_cart on the cart page");</script>';
}
} // End class
$Demowoo = new Demowoo();
if($Demowoo) {
register_activation_hook( __FILE__, array(&$Demowoo, 'install'));
}
}
All your calls of add_action should use the form array($this, 'method_name') in their second parameter, like the first hook on wp_footer. This is because you are hooking methods of an object, not functions.
If you just write the name of the method WP will look for a global function with that name, not a class method. Since there are no global functions with those names, nothing happens.
The array syntax allows WP to know not only the method name but which object it should be invoked from. PHP cannot just invoke an object method without having an instance of the object, so you need to provide one to the hooking system. With the style to define plugins that you're using here the object instance is usually always $this.

WP Elegant Themes > hide tinymce buttons?

I have an elegant theme installed in my Wordpress website, and I am wondering how i can hide or remove the shortcode buttons in the tinymce (thing) that are generated by the Elegant Theme (admin area WP). I have been trying to look up the action hook and remove it and also play with the CSS of the buttons, but nothing helped. any idea's on how to remove these small buttons from the backend of my WP website?
This is the related code I've found:
add_filter('mce_buttons', 'et_filter_mce_button');
add_filter('mce_external_plugins', 'et_filter_mce_plugin');
function et_filter_mce_button($buttons) {
array_push( $buttons, '|', 'et_learn_more', 'et_box', 'et_button', 'et_tabs', 'et_author' );
return $buttons;
}
Create a plugin and remove the filters:
<?php
/**
* Plugin Name: Remove ET MCE Buttons
*/
add_action( 'admin_init', 'remove_et_so_19084867' );
function remove_et_so_19084867() {
remove_filter( 'mce_buttons', 'et_filter_mce_button' );
remove_filter( 'mce_external_plugins', 'et_filter_mce_plugin' );
}
A user from the Elegant Themes forum referenced this post: https://wordpress.stackexchange.com/questions/103347/removing-buttons-from-the-editor and said:
Place the following code in your child theme functions.php:
// HIDE TINYMCE CUSTOM BUTTONS
function tinymce_editor_buttons($buttons) {
return array();
}
function tinymce_editor_buttons_second_row($buttons) {
return array();
}
add_filter("mce_buttons", "tinymce_editor_buttons", 99);
add_filter("mce_buttons_2", "tinymce_editor_buttons_second_row", 99);
This removes all buttons inserted by Divi and other plugins. If you need to keep any buttons you can include the corresponding ID inside of return array();. Also, if you're using the plugin TinyMCE Advanced, the standard buttons stay in place anyway.

Unregister WordPress plugin widget from theme

Question about WordPress theme - plugins interaction.
Can i unregister widget that was added by plugin using theme functions.php file?
Tried to unregister it using sample code, but it didn't work for me:
function remove_some_widget() {
unregister_widget('some_plugin_widget');
}
add_action( 'widgets_init', 'remove_some_widget' );
The parameter to pass in unregister_widget function is the name of a class that extends, so, pass the appropriate class name. Here is an example of unregister_widget to Unregister all widgets
function unregister_default_widgets() {
unregister_widget('WP_Widget_Pages');
unregister_widget('WP_Widget_Calendar');
unregister_widget('WP_Widget_Archives');
unregister_widget('WP_Widget_Links');
unregister_widget('WP_Widget_Meta');
unregister_widget('WP_Widget_Search');
unregister_widget('WP_Widget_Text');
unregister_widget('WP_Widget_Categories');
unregister_widget('WP_Widget_Recent_Posts');
unregister_widget('WP_Widget_Recent_Comments');
unregister_widget('WP_Widget_RSS');
unregister_widget('WP_Widget_Tag_Cloud');
unregister_widget('WP_Nav_Menu_Widget');
unregister_widget('Twenty_Eleven_Ephemera_Widget');
}
add_action('widgets_init', 'unregister_default_widgets', 11);

Resources