Wordpress Shortcode syntax error, unexpected '}' - wordpress

I am learning how to make wordpress theme. I am doing pretty well. Now I want to make shortcodes. But is is showing Parse error: syntax error, unexpected '}' in E:\xampp\htdocs\wordpress\wp-content\themes\freedom\shortcodes.php on line 17 . I still don't know what is the problem. Here is the code .
<?php
function slider_function( $atts ) {
$atts = extract( shortcode_atts(
array(
'tittle'=>'',
'description'=>'',
'button'=>'',
'button_url'=>''
),$atts ) );
return '
<div class="slider">
<h1>"' . $atts['tittle'] . '"</h1>
<h2>"' . $atts['tittle'] . '"</h2>
</div>'
}
add_shortcode( 'slider','slider_function' );
?>

Take a look at this code how shortcode works and look at your code what returning.
function bartag_func( $atts ) {
$a = shortcode_atts( array(
'foo' => 'something',
'bar' => 'something else',
), $atts );
return "foo = {$a['foo']}";
}
add_shortcode( 'bartag', 'bartag_func' );

You forgot a semicolon /div>';.
function slider_function( $atts ) {
$atts = extract( shortcode_atts(
array(
'tittle'=>'',
'description'=>'',
'button'=>'',
'button_url'=>''
),$atts ) );
return '
<div class="slider">
<h1>"' . $atts['tittle'] . '"</h1>
<h2>"' . $atts['tittle'] . '"</h2>
</div>';
}
add_shortcode( 'slider','slider_function' );

Related

Add different WordPress excerpt formats to different templates

I added the following code to my functions.php file in WordPress 6.1.1 to display excerpts.
function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
function new_excerpt_more($more) {
return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');
...but I also have a use case to show the full excerpt without a read more link.
On page template 1 I add the below code to display the excerpt:
<?php echo the_excerpt(); ?>
...and it displays the excerpt as per the functions.php file but how do I create a 2nd excerpt without the read more link and apply it to page template 2?
Is there a parameter I can use within the_excerpt(parameter); or can I use something like wp_trim_excerpt https://developer.wordpress.org/reference/functions/wp_trim_excerpt/ maybe?
I came across the below code that is supposed to do what I want
function wpex_get_excerpt( $args = array() ) {
// Default arguments.
$defaults = array(
'post' => '',
'length' => 40,
'readmore' => false,
'readmore_text' => esc_html__( 'read more', 'text-domain' ),
'readmore_after' => '',
'custom_excerpts' => true,
'disable_more' => false,
);
// Apply filters to allow child themes mods.
$args = apply_filters( 'wpex_excerpt_defaults', $defaults );
// Parse arguments, takes the function arguments and combines them with the defaults.
$args = wp_parse_args( $args, $defaults );
// Apply filters to allow child themes mods.
$args = apply_filters( 'wpex_excerpt_args', $args );
// Extract arguments to make it easier to use below.
extract( $args );
// Get the current post.
$post = get_post( $post );
// Get the current post id.
$post_id = $post->ID;
// Check for custom excerpts.
if ( $custom_excerpts && has_excerpt( $post_id ) ) {
$output = $post->post_excerpt;
}
// No custom excerpt...so lets generate one.
else {
// Create the readmore link.
$readmore_link = '' . $readmore_text . $readmore_after . '';
// Check for more tag and return content if it exists.
if ( ! $disable_more && strpos( $post->post_content, '<!--more-->' ) ) {
$output = apply_filters( 'the_content', get_the_content( $readmore_text . $readmore_after ) );
}
// No more tag defined so generate excerpt using wp_trim_words.
else {
// Generate an excerpt from the post content.
$output = wp_trim_words( strip_shortcodes( $post->post_content ), $length );
// Add the readmore text to the excerpt if enabled.
if ( $readmore ) {
$output .= apply_filters( 'wpex_readmore_link', $readmore_link );
}
}
}
// Apply filters and return the excerpt.
return apply_filters( 'wpex_excerpt', $output );
}
Output using:
<?php echo wpex_get_excerpt ( $defaults = array(
'length' => 40,
'readmore' => true,
'readmore_text' => esc_html__( 'read more', 'wpex-boutique' ),
'custom_excerpts' => true,
) ); ?>
...but doesn't seem to workas intended. Outputs the excerpt with no link but the args don't see to work when changed. Would be perfect for my use otherwise. Maybe someone sees how to fix this code?
Thanks

How to ensure shortcode result doesn't break formatting?

I want to add a shortcode which will execute a DB query and return the result.
Here's my functions.php:
function get_posts_count($cat){
global $wpdb;
$a = shortcode_atts( array(
'id' => ''
), $cat );
$id=$a['id'];
$count=$wpdb->get_results( "SELECT `count` FROM `wpmy_term_taxonomy` WHERE `term_id`=$id");
foreach($count as $row)
echo '('.$row->count.')';
}
add_shortcode( 'postCount', 'get_posts_count' );
This is the shortcode in the editor:
And here's the end result:
The value in this case 1 appears above the text Real Estate. How can I make sure it is displayed within the line?
Thanks in advance
the shortcode accepts parameters (attributes) and return a result (the shortcode output). If the shortcode produces a HTML then ob_start can be used to capture output and convert it to a string as follows:-
function get_posts_count( $cat ) {
ob_start();
global $wpdb;
$a = shortcode_atts( array(
'id' => '',
), $cat );
$id = $a['id'];
$count = $wpdb->get_results( "SELECT `count` FROM `wpmy_term_taxonomy` WHERE `term_id`=$id" );
foreach ( $count as $row ) {
echo '(' . $row->count . ')';
}
return ob_get_clean();
}
add_shortcode( 'postCount', 'get_posts_count' );

How to display woocommerce PRICE on product's BODY text - using SHORTCODE?

I'm writing some details of each prodcuts (short/long description) on woocommerce.
i'd like to insert a SHORTCODE inside the description that shows the current PRICE (sale / regular).
it should look something like that on the backend:
"Buy it now, for only [wc_price] $"
is there any shortcode i can use for that?
This is one the most simple snippet that does what you want without the need of inputting the id manually:
function my_shortcode_product_price() {
$html = '';
global $product;
$price = wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) );
$args = array(
'ex_tax_label' => false,
'currency' => 'USD',
'decimal_separator' => '.',
'thousand_separator' => ' ',
'decimals' => 2,
'price_format' => '%2$s %1$s',
);
$html = "<span>" . wc_price( $price, $args ) . "</span>";
return $html;
}
add_shortcode( 'product_price', 'my_shortcode_product_price' );
The above code goes in the functions.php file of your active theme. After that you can use the shortcode like this:
[product_price]
Here You Go: add this code in your function.php
function short_code_woo_comm_desc( $atts ) {
$atts = shortcode_atts( array(
'id' => null
), $atts, 'tag_for_short_code_price' );
if ( empty( $atts[ 'id' ] ) ) {
return '';
}
$product = wc_get_product( $atts['id'] );
if ( ! $product ) {
return '';
}
return $product->get_price_html();
}
add_shortcode( 'tag_for_short_code_price', 'short_code_woo_comm_desc' );
Use:
[tag_for_short_code_price id="101"]

Shortcode for button with custom style

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]

Shortcode displaying strangely

I'm trying to get a shortcode to add an id to a div, and that part works fine, but it adds some extra stuff and I'm not sure if it's because I did something wrong, or if it's something else. Here is the actual shortcode:
function jump_function($params){
//Extract parameters and supply default values
extract( shortcode_atts( array(
'id' => ''
), $params ) );
//The parameters are now stored as variables
return do_shortcode('<div id="' . $id . '"</div>');
}
add_shortcode( 'jump', 'jump_function' );
I'm trying to get it to display as
<div id="id-here"></div>
but on the page it's displaying as this:
<div div="" <="" id="id-here" style="position: relative;"><div></div></div>
Did I do anything wrong?
do_shortcode doesn't work like this...
you don't need it.
function jump_function($params){
//Extract parameters and supply default values
extract( shortcode_atts( array(
'id' => ''
), $params ) );
//The parameters are now stored as variables
return '<div id="' . $id . '"</div>';
}
add_shortcode( 'jump', 'jump_function' );
http://codex.wordpress.org/do_shortcode

Resources