I'm trying out a plugin test where I've created a simple plugin that will wrap text in a <span> element with a class, when I in the editor wrap the text in a shortcode.
It works. But the css styling in the style.css file for that plugin isn't applied.
The plugin script is this:
class shs_wrap {
/*Create shortcode [wrap]*/
function shs_wrap() {
add_shortcode( 'wrap', array( &$this, 'shortcode' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'style' ) );
}
/*Run the shortcode*/
function shortcode( $atts , $content = null) {
/*If the "class" is forgotten in the shortcode, abort*/
if( empty(preg_quote($atts['class'])) || empty($content))
return;
/*Prepare output*/
$output = "<span class='".$atts['class']."'>" . $content . "</span>";
return $output;
}
/*Add styling from the css-file*/
function style() {
wp_register_style( 'style', plugins_url( 'style.css', __FILE__ ));
wp_enqueue_style( 'style' );
}
}
new shs_wrap();
When writing
Here is some text [wrap class="test"]here is some wrapped text[/wrap] here is some more text
I simply want the output to be:
Here is some text <span class="test">here is some wrapped text</span> here is some more text
My script above works and I can see in the source code that the <span> element is correctly added with its class - here is a screenclip from the inspection tool:
The css styling from the style.css file is not loaded, though. The output on the screen is not styled and the style file is never added. The style file is located at the same location as the php-file (called shs-wrap.php):
and it contains only this tiny css snippet:
What is the issue here? Is there an error in my style enqueue function? Do I call it wrongly?
May just be me but I think you should use __construct like below in addition to a more unique style handle:
class shs_wrap {
/*Create shortcode [wrap]*/
function __construct() {
add_shortcode( 'wrap', array( $this, 'shortcode' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'style' ) );
}
/*Run the shortcode*/
function shortcode( $atts , $content = null) {
/*If the "class" is forgotten in the shortcode, abort*/
if( empty(preg_quote($atts['class'])) || empty($content)) {
return;
}
/*Prepare output*/
$output = "<span class='".$atts['class']."'>" . $content . "</span>";
return $output;
}
/*Add styling from the css-file*/
function style() {
wp_register_style( 'my-plugin-test-style', plugins_url( 'style.css', __FILE__ ));
wp_enqueue_style( 'my-plugin-test-style' );
}
}
new shs_wrap();
Related
In previous when I develop WP shortcode then I load the CSS style depend on the shortcode params like this.
function test_shortcode( $atts ) {
$a = shortcode_atts( array(
'foo' => 'something',
), $atts );
echo "<style>";
.shortcode_unique_id {
//some css property depend on shortcode
}
echo "</style>";
}
But now it did not allow to add any style tag in the body otherwise it will show w3 validation error. We need to load style in the head tag.
Please anyone tell me how to load CSS style depend on shortcode params when calling the shortcode.
Advance Thanks
You can use wp_register_style and wp_enqueue_style to use style which is dependent on shortcode.
Main difference between wp_enqueue_* and respective wp_register_* functions, is that the first adds scripts/styles to the queue, the second prepares scripts/styles to be added.
The typical scenario of using both functions is when you want to register scripts/styles on theme init, and then enqueue them wherever necessary e.g.
add_action('init', 'my_register_styles');
function my_register_styles() {
wp_register_style( 'style1', get_template_directory_uri() . '/style1.css' );
wp_register_style( 'style2', get_template_directory_uri() . '/style2.css' );
wp_register_style( 'style3', get_template_directory_uri() . '/style3.css' );
}
/*This is you shortcode function*/
function test_shortcode( $atts ) {
$a = shortcode_atts( array(
'foo' => 'something',
), $atts );
wp_enqueue_style( 'style1' );
wp_enqueue_style( 'style2' );
wp_enqueue_style( 'style3' );
}
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 have the following pieces of code that I use to include styles and java-scripts on a settings page within the WordPress admin area. I have a class with a singleton that I use to initiate my admin pages. I stripped everything except the needed pieces of code to make it more readable.
The problem that I'm having is that by using the set-up below is that the style-sheets on the settings page are placed at the bottom of the page instead of in the head of the page. I can get it in the header by using other action hooks, but that would defeat the purpose. As far as I know the set-up I used is the same setup as is described with the wp_enqueue_style command.
There is a small hint with the command "wp_enqueue_style() can now be called mid-page (in the HTML body). This will load styles in the footer.". If that is true that would mean that the 'admin_print_scripts-*' hook is called somewhere mid-page instead of at the start en doing so places the css in the footer.
Any thoughts on that. an I doing something wrong ?
Thanks for your time.
This is how the singleton class is called within the functions.php file
theme::instance( );
This is part of the class that I used to create the admin pages
class theme {
static public function instance( )
{
is_null( self::$instance ) AND self::$instance = new self;
return self::$instance;
}
public function __construct()
{
add_action( 'admin_menu', array( $this, 'initMenu' ), 10, 0 );
add_action( 'admin_init', array( $this, 'registerAssets' ), 10, 0 );
}
public function registerAssets( )
{
// Styles
wp_register_style( 'cen', 'style.css', array( ), '1.0' );
wp_register_style( 'cen-settings', 'settings.css', array( 'cen' ), '1.0' );
// Scripts
wp_register_script( 'cen', 'settings.js', array( 'jquery' ), '1.0', true );
}
public function initMenu( )
{
// Index page
$index =add_menu_page( 'Cen', 'Cen', 'manage_options', 'cen-index', function (){ require_once( get_template_directory( ) . '/pages/index.php' ); }, get_template_directory_uri() . '/images/icons/logo_16.png', "110.00" );
// Settings page
$settings =add_submenu_page( 'cen-index', 'Cen - Settings', 'cen' ), 'Settings', 'manage_options', 'cen-settings', function (){ require_once( get_template_directory( ) . '/pages/settings.php' ); } );
// Add action for assets on the settings page
add_action( 'admin_print_scripts-' . $settings, array( $this, 'initSettingsPage' ));
}
public function initSettingsPage( )
{
// Styles used
wp_enqueue_style( 'cen' );
wp_enqueue_style( 'cen-settings' );
// Scripts used
wp_enqueue_script( 'jquery-ui-tabs' );
wp_enqueue_script( 'cen' );
}
}
The action hook admin_print_scripts you're using is used to add inline script so it's strongly recommended to use admin_enqueue_scripts to enqueue scripts/styles in the admin.
Try it. Hope it works!
I have already added an link in my widget
eg:
<a target="_blank" href="google.com" class="a-button">Learn More</a>
I need an shortcode like this
[button link="google.com" value="Learn More"]
If i paste this shortcode in widget, page and post, link must appear
Style must be same as above tag
Current Code:
function button_shortcode($atts, $content = null) {
extract( shortcode_atts( array( 'url' => '#' ), $atts ) );
return '' . do_shortcode($content) . '';
}
add_shortcode('button', 'button_shortcode');
How can i do this ?
Basic shortcode will look like this:
function a_button_shortcode( $atts, $content = null ) {
extract($atts);
return '<a target="_blank" href="' . esc_attr($link) . '" class="a-button">' . esc_attr($value) . '</a>';
}
add_shortcode( 'button', 'a_button_shortcode' );
You can read more on shortcode api on: http://codex.wordpress.org/Shortcode_API
In order to make your widget with shortcodes should use do_shortcode( $content ) function inside update method of your widget.
Like this:
function update( $old_instance, $new_instance) {
$new_instance['content'] = do_shortcode($new_instance['content']);
return $new_instance;
}
Or use a plugin that will make it for default widgets like this https://wordpress.org/plugins/shortcodes-in-sidebar-widgets/
add_shortcode("init" ,"add_custom_shortcode");
function add_custom_shortcode()
{
add_shortcode('button', 'buttonShortcode');
}
function buttonShortcode($atts, $text='') {
extract( shortcode_atts( array( 'url' => '#' ), $atts ) );
return '' . do_shortcode($text) . '';
}
thank you all, i tried out this one
function button_shortcode($atts, $content = null) {
extract( shortcode_atts( array(
'url' => '#'
), $atts ) );
return '' . do_shortcode($content) . '';
}
add_shortcode('button', 'button_shortcode');\
For shortcode support in widget paste below line in functions.php
add_filter('widget_text', 'do_shortcode');
created shortcode:
[button url="google.com"]Download[/button]
i׳m trying to create an accordion shortcode.
when I load the scripts with wp_enqueue_script function it seams to work only for the jquery ui library but not the custom script
jQuery(document).ready(
function() {
$('#ks-accordion').accordion();
}
);
Here is the full code:
// Container
function ks_accordion($atts, $content = null) {
extract( shortcode_atts( array(
'id' => ''
), $atts ) );
wp_enqueue_script('jquery-ui-accordion');
wp_enqueue_script('shortcode', get_template_directory_uri().'/functions/shortcodes/js/shortcodes.js');
return '<div id="accordion">'.do_shortcode($content).'</div>';
}
add_shortcode( 'accordion', 'ks_accordion' );
// Section
function ks_accordion_section($atts, $content = null) {
extract( shortcode_atts( array(
'title' => 'My Title',
), $atts ) );
return '<h6>'.$title.'</h6><div><p>'.do_shortcode($content).'</p></div>';
}
add_shortcode( 'accordion_section', 'ks_accordion_section' );
This is the code I get in the front-end:
<div id="ks-accordion"><br>
<h6>Title</h6><div><p>Content</p></div><br>
<h6>Title1</h6><div><p>Content</p></div><br>
<h6>Title2</h6><div><p>Content</p></div><br>
</div>
Also I don't get why it creates these <br>?
Thanks
jQuery is loaded in noConflict mode in WordPress. To use the dollar sign, we need the following
jQuery(document).ready( function($) { // <---- put $ here
$('#ks-accordion').accordion();
});
Much probably, do_shortcode is inserting those <br>, but it's just speculation.
wp_enqueue_script('shortcode', get_template_directory_uri().'/functions/shortcodes/js/shortcodes.js', array('jquery'));
Hope this helps!