WordPress load dynamic css only on frontend - css

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

Related

add_shortcode in a plugin

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();
}

Multiple WooCommerce Single Product Templates

Following these instructions to have WooCommerce load a different 'single-product' template:
if( has_term( 'termgoeshere', 'product_cat' ) ) {
$file = 'single-product-newtempforterm.php';
} else {
$file = 'single-product.php';
}
global $woocommerce;
load_template( $woocommerce->template_url . $file );
I'm experiencing an error:
Warning: require_once(woocommerce/single-product-wide.php): failed to open stream: No such file or directory in /home/xxx/public_html/wp-includes/template.php on line 572
Fatal error: require_once(): Failed opening required 'woocommerce/single-product-wide.php' (include_path='.:/usr/local/php54/pear') in /home/xxx/public_html/wp-includes/template.php on line 572
I'm not sure where to begin to fix the problem.
You can just use wc_get_template_part() instead of $woocommerce->template_url.
Keep in mind that you don't need .php for that.
Use a single-product-custom.php template for any product in the "custom" category:
add_filter( 'template_include', 'so_43621049_template_include' );
function so_43621049_template_include( $template ) {
if ( is_singular('product') && (has_term( 'custom', 'product_cat')) ) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-custom.php';
}
return $template;
}
NB: If you use the same action hooks in your single-product-custom.php template you will get the same look as the default single-product.php. You could 'rename' all the hooks and then could add existing functions (such as those for add to cart buttons, etc) to the new hooks in order to achieve a totally custom look.

Automatically Deactivate Plugin After a Check

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

Wordpress : Conditional Tags Within Plugin

Why can't use conditonal tags inside plugin? Here's the script:
/* enqueue */
function portfoliodetail_enqueue() {
wp_enqueue_script( 'portfoliodetailjs', plugin_dir_url(__FILE__) .'js/portfolio-detail.js', array('velocity'), null, true );
}
if ( is_singular( 'portfolio' ) ) {
add_action( 'wp_enqueue_scripts', 'portfoliodetail_enqueue' );
}
/* /enqueue */
the is_singular( 'portfolio' ) (because the post type is portfolio) works well in theme directory file. But when it is written within plugin, it doesn't work.
Anybody willing to help?
Your condition should be in the hook.
it should be like this:
function portfoliodetail_enqueue() {
if ( is_singular( 'portfolio' ) ) {
wp_enqueue_script( 'portfoliodetailjs', plugin_dir_url(__FILE__) .'js/portfolio-detail.js', array('velocity'), null, true );
}
}
add_action( 'wp_enqueue_scripts', 'portfoliodetail_enqueue' );

Is there any way to override a WordPress template with a plugin?

I'd like to make a landing page. If plugin detects some GET or POST requests it should override wordpress theme and show its own.
It would work somehow like that:
if (isset($_GET['action']) && $_GET['action'] == 'myPluginAction'){
/* do something to maintain action */
/* forbid template to display and show plugin's landing page*/
}
I'm familiar with WP Codex, but I don't remember if there is any function to do that. Of course, I googled it with no results.
Thanks for any ideas in advance.
You need the hook template_include. It doesn't seem documented in the Codex, but you can find more examples here in SO or in WordPress StackExchange
Plugin file
<?php
/**
* Plugin Name: Landing Page Custom Template
*/
add_filter( 'template_include', 'so_13997743_custom_template' );
function so_13997743_custom_template( $template )
{
if( isset( $_GET['mod']) && 'yes' == $_GET['mod'] )
$template = plugin_dir_path( __FILE__ ) . 'my-custom-page.php';
return $template;
}
Custom Template in Plugin folder
<?php
/**
* Custom Plugin Template
* File: my-custom-page.php
*
*/
echo get_bloginfo('name');
Result
Visiting any url of the site with ?mod=yes will render the plugin template file, e.g.: http://example.com/hello-world/?mod=yes.
you need to create a folder '/woocommerce/' inside your plugin directory, inside woocommerce you need to add a folder say, for email 'emails' and put the required template inside '/emails/' to override. just copy paste this code in the main.php of your plugin.
<?php
/**
* Plugin Name: Custom Plugin
*/
function myplugin_plugin_path() {
// gets the absolute path to this plugin directory
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
$_template = $template;
if ( ! $template_path ) $template_path = $woocommerce->template_url;
$plugin_path = myplugin_plugin_path() . '/woocommerce/';
// Look within passed path within the theme - this is priority
$template = locate_template(
array(
$template_path . $template_name, $template_name
)
);
// Modification: Get the template from this plugin, if it exists
if ( ! $template && file_exists( $plugin_path . $template_name ) )
$template = $plugin_path . $template_name;
// Use default template
if ( ! $template )
$template = $_template;
// Return what we found
return $template;
}
?>
for reference template override using plugin

Resources