I have a problem adding a shortcode in a custom plugin.
The custom plugin code is:
<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/*
Plugin Name: ......
Plugin URI: ..........
description:
..........
Version: 1.2
Author: .............
Author URI: ...........
License: GPL2
*/
function sdsdsd_my_shortcode(){
return '9999999999999999';
}
add_action( 'init', 'sdsdsd_add_shortcode' );
function sdsdsd_add_shortcode() {
add_shortcode( 'sdsdsd_my_shortcode', 'sdsdsd_my_shortcode' )
}
The in WP admin edit post: [sdsdsd_my_shortcode], but instead the content, it renders exactly [sdsdsd_my_shortcode]
The plugin is registered and activated in WP admin/plugins.
The same code works fine in functions.php in the child theme.
The plugin is plugins/my-custom-plugin/my-custom-plugin.php
In the plugin file, it doesn't work and without add_action( 'init',...
Try This --
if ( ! defined( 'ABSPATH' ) ) {
die( 'You are not allowed to call this page directly.' );
}
if ( ! class_exists( 'Class_Name' ) ) {
class Class_Name{
public function __construct() {
$this->register_hooks();
}
function register_hooks() {
add_shortcode( 'sdsdsd_my_shortcode', array($this, 'sdsdsd_shortcode_content') );
}
function sdsdsd_shortcode_content() {
ob_start();
//Content
<?php
$form = ob_get_contents();
ob_clean();
return $form;
}
}
return new Class_Name();
}
Related
These two srcripts are added on every footer of my WordPress pages. Is it possible to remove them via functions.php?
The following code in your functions.php should remove wp-polyfill and regenerator-runtime:
function deregister_polyfill(){
wp_deregister_script( 'wp-polyfill' );
wp_deregister_script( 'regenerator-runtime' );
}
add_action( 'wp_enqueue_scripts', 'deregister_polyfill');
See also: wp_deregister_script
Edit: After further investigation on the problem, I found that these styles are included by the Contact Form 7 plugin. My solution to the problem was to write a Must Use Plugin so that the Contact Form 7 plugin is only included when you are in the contact form.
For this purpose I have created the following file:
wp-content/mu-plugins/tk-mu-plugins.php
which is filled as follows:
<?php
/*
Plugin Name: TK-MU-Plugin
Description:
Author: Thomas Krakow
Version: 1.0
Author URI: https://thomas-krakow.de
*/
$fRequestURI = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
if( false === strpos( $fRequestURI, '/wp-admin/' ) ){
add_filter( 'option_active_plugins', 'tk_activate_plugins' );
}
function tk_activate_plugins( $plugins ){
global $fRequestURI;
$is_contact_page = strpos( $fRequestURI, '/contact/' );
$k = array_search( "contact-form-7/wp-contact-form-7.php", $plugins );
if( false !== $k && false === $is_contact_page ){
unset( $plugins[$k] );
}
return $plugins;
}
My Code at GitHub Gist: tk-mu-plugin.php
Explanation in German on my blog: CSS und Javascript nur bei Bedarf auf WordPress-Seiten einfügen
I have some functions to override in my child theme but keep on getting Cannot redeclare langs_content().
I used quite a few resources to troubleshoot and something that shoud be straight forward I can't work it out
<?php
function enqueue_parent_theme_style() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_parent_theme_style' );
function enqueue_child_theme_style() {
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri().'/style.css' );
}
//require_once( get_stylesheet_directory() . '/functions.php');
add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_style', 100 );
if ( ! function_exists( 'langs_content' ) ) {
/*
* My Awesome function is awesome
*
*/
function langs_content( $atts, $content = null ) // shortcode lang
{
$lang = the_lang();
extract( shortcode_atts( array(), $atts ) );
$langs_content = '';
if(in_array($lang, $atts))
{
$langs_content .= do_shortcode($content);
}
return $langs_content;
}
add_shortcode('lang', 'langs_content');
}
?>
Really simple ! Remove the shortcode from init hook then add your new one ;-)
Like this :
/******
* Inside parent functions.php (don't touch this!)
******/
function test_shortcode() {
return "Hello World from parent theme";
}
add_shortcode('test_shortcode', 'test_shortcode');
/******
* Inside child functions.php
******/
function child_test_shortcode()
{
return "Hello World from child theme";
}
function remove_parent_theme_shortcodes()
{
remove_shortcode('test_shortcode');
// Add our new function with the same shortcode
add_shortcode('test_shortcode', 'child_test_shortcode');
}
// Remove the shortcode with init hook
add_action('init', 'remove_parent_theme_shortcodes');
I am trying to give a graceful custom message upon activation of the plugin, if Woocommerce is not activated.
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
The above express is to check whether Woocommerce is active and if it returns false, I do not want the plugin to be activated and wants to throw a custom error. I have tried to stop execution with die() and trigger_error. In these cases, it shows a FATAL ERROR.
What I usually do is throw and admin notice if WooCommerce isn't available. And then just stop the running of your plugin.
if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ){
add_action( 'admin_notices', 'so_32551934_woocommerce_warning' );
return; // STOP THE WHOLE PLUGIN
}
function so_32551934_woocommerce_warning(){ ?>
<div class="error">
<p><?php _e( 'Whoa, you\'re gonna need WooCommerce to run my fancy plugin!', 'my-text-domain' ); ?></p>
</div>
<?php
}
// The rest of your plugin follows here
Any code that you put here at the end will be run as long as WooCommerce is active. Note, I didn't test this so be careful of typos.
Have a look at deactivate_plugins function . Try this:
class MyPlugin {
public function __construct() {
register_activation_hook( __FILE__, array( $this , 'activate' ) );
}
public function activate() {
// Check if Woo is active
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
// Woo is active.
} else {
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( __( 'Please activate WooCommerce.', 'my-plugin' ), 'Plugin dependency check', array( 'back_link' => true ) );
}
// Do activate Stuff now.
}
}
new MyPlugin();
Here my solution:
class FT_AdminNotice {
/**
* Register the activation hook
*/
public function __construct() {
register_activation_hook( __FILE__, array( $this, 'ft_install' ) );
}
/**
* Deactivate the plugin and display a notice if the dependent plugin is not active.
*/
public function ft_install() {
if ( ! class_exists( 'WooCommerce' ) )
{
$this->ft_deactivate_plugin();
wp_die( sprintf(__( 'This plugin requires Woocommerce to be installed and activated.', 'domain-text' ) ) );
}
}
/**
* Function to deactivate the plugin
*/
protected function ft_deactivate_plugin() {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
deactivate_plugins( plugin_basename( __FILE__ ) );
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
}
new FT_AdminNotice();
This way show message and you can enrich it like explain here and stop execution too.
I'm trying to get my Wordpress plugin to deactivate automatically after a simple check. It seems to be calling the admin_notices method just fine, but the deactivate_plugin() method does not do anything. This is in the class constructor:
// End if the theme isn't compatible
if ( FALSE == $this->themesupport['support'] ) { // This test works fine
add_action( 'admin_init', array( &$this, 'deactivate_plugin' ) ); // Plugin doesn't deactivate
add_action( 'admin_notices', array( &$this, 'admin_notices' ) ); // I get notices
if ( isset( $_GET['activate'] ) )
unset( $_GET['activate'] );
return;
} // if()
The method is pretty straightforward:
public function deactivate_plugin() {
deactivate_plugins( plugin_basename( __FILE__ ) );
} // deactivate_plugin()
Putting an echo in that deactivate_plugin method and it gets called. I've also tried including the plugins.php file from core with no change.
The following works:
<?php
/**
* Plugin Name: (SO) Self-deactivate with $_GET['my_deactivate']
*/
add_action( 'admin_init', function()
{
if( isset( $_GET['my_deactivate'] ) )
{
deactivate_plugins( plugin_basename( __FILE__ ), true );
$url = admin_url( 'plugins.php?deactivate=true' );
header( "Location: $url" );
die();
}
});
After activating, enter any admin URL adding ?my_deactivate, e.g.:
http://example.com/wp-admin/users.php?my_deactivate
Reference:
Function deactivate_plugins does not exist
I was calling deactivate_plugins() from within a class, rather than the plugin file itself, which of course returned the wrong location for FILE
My problem is that I use dynamic css file in the WordPress theme that is loaded with Ajax. However, it loads this same dynamic css file for backend also. How do I modify my code that it loads dynamic css file only for frontend, not for the backend. Here's my code:
wp_enqueue_style('dynamic-css',
admin_url('admin-ajax.php?action=dynamic_css'));
function dynaminc_css() {
require(get_template_directory().'/dynamic-css.php');
exit;
}
add_action( 'wp_ajax_dynamic_css', 'dynaminc_css' );
add_action( 'wp_ajax_nopriv_dynamic_css', 'dynaminc_css' );
}
Here's a working example with inline comments:
<?php
/*
Plugin Name: Dynamic CSS using Ajax
Plugin URI: https://github.com/soderlind/
Description:
Author: Per Soderlind
Version: 0.1.0
Author URI: http://soderlind.no
*/
if ( !defined( 'ABSPATH' ) ) {
die( 'Cheating, are we?' );
}
define( 'DYNAMICCSS_VERSION', '0.1.0' );
function dynamic_css_enqueue() {
wp_enqueue_style( 'dynamic-flags', admin_url( 'admin-ajax.php' ).'?action=dynamic_css&_wpnonce=' . wp_create_nonce( 'dynamic-css-nonce' ), false, DYNAMICCSS_VERSION );
}
function dynamic_css() { // Don't wrap function dynamic_css() in if(!is_admin()){ , the call from admin-ajax.php will be from admin
$nonce = $_REQUEST['_wpnonce'];
if ( ! wp_verify_nonce( $nonce, 'dynamic-css-nonce' ) ) {
die( 'invalid nonce' );
} else {
/**
* NOTE: Using require or include to call an URL ,created by plugins_url() or get_template_directory(), can create the following error:
* Warning: require(): http:// wrapper is disabled in the server configuration by allow_url_include=0
* Warning: require(http://domain/path/flags/css.php): failed to open stream: no suitable wrapper could be found
* Fatal error: require(): Failed opening required 'http://domain/path/css.php'
*/
require dirname( __FILE__ ) . '/css.php'; //use echo, printf etc in css.php and write to standard out.
}
exit;
}
add_action( 'wp_ajax_dynamic_css', 'dynamic_css' );
add_action( 'wp_ajax_nopriv_dynamic_css', 'dynamic_css' );
add_action( 'wp_enqueue_scripts', 'dynamic_css_enqueue' ); //wp_enqueue_scripts = load on front-end
The is_admin() function is what you are looking for
if(!is_admin()){
wp_enqueue_style('dynamic-css',
admin_url('admin-ajax.php?action=dynamic_css'));
function dynaminc_css() {
require(get_template_directory().'/dynamic-css.php');
exit;
}
add_action( 'wp_ajax_dynamic_css', 'dynaminc_css' );
add_action( 'wp_ajax_nopriv_dynamic_css', 'dynaminc_css' );
}
Anything inside there will only execute if not in the administration panel.
http://codex.wordpress.org/Function_Reference/is_admin