How to insert shortcode with more parameters and multiline into WordPress page?
Backslash used to work as a line separator:
[my_shorcode parameter_1=10 \
parameter_2=20 \
parameter_3="test"]
But it doesn't work in the latest WordPress version. I want to use more lines for better readability of the code.
Thanks for #Arvind K. for advice
This is not good idea since it breaks normal post editing behavior.
Rather you should try to keep shortcodes "short
I've been solving this problem by the way
1. I have used shortcode this way [my-shortcode id="1"]
2. I have created post type My Shortcodes
3. The id for step 1. is the My Shortcodes post id
4. In ACF (Custom fields) I have created fields for My Shortcodes
(Field - is shortcode param)
5. Then you could use your My Shortcodes post for getting fields.
add_shortcode( 'my-shortcode', function ( $atts, $content ) {
$custom_shortcode_id = $atts['id'];
$parameter_1 = get_field( 'parameter_1', $custom_shortcode_id );
$parameter_2 = get_field( 'parameter_2', $custom_shortcode_id );
return "Your shortcode content $parameter_1, $parameter_2";
} );
I removed 2 WordPress filters and it works fine now.
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
Related
I am trying to add a shortcode to all my woocommerce single product pages. I want the shortcode to appear above the short description.
I have found this code and added it to functions.php, but I can see that it adds the shortcode below the short description (and above the add to cart button).
add_filter('woocommerce_short_description','ts_add_text_short_descr');
function ts_add_text_short_descr($description){
$text = do_shortcode('[my-shortcode]');
return $description.$text;
}
I would be very gratefull for some help :)
function customShortcodeBeforeShortDescription( $post_excerpt )
{
echo do_shortcode('[wpqr-code]');
}
add_filter('woocommerce_short_description','customShortcodeBeforeShortDescription', 10, 1);
You can use the woocommerce_single_product_summary action hook. try the below code. The code will go in your active theme functions.php file.
function add_shortcode_before_short_description(){
echo do_shortcode( '[wpqr-code]' );
}
add_action( 'woocommerce_single_product_summary', 'add_shortcode_before_short_description', 15, 1 );
Tested and works
I'm creating some custom dynamic blocks for WordPress Gutenberg editor (following this link ).
I use the PHP render for these blocks, meaning I have this code on save:
save: function( props ) {
// Rendering in PHP
return;
},
The render function is called via this callback:
register_block_type( 'my-plugin/latest-post', array(
'render_callback' => 'my_plugin_render_block_latest_post',
) );
I'm not gonna post the function code since is irrelevant in this case. (I do
a WP_Query and display some custom post data and return a html code),
My problem is that WP Gutenberg takes the output from the function and adds
<p> and <br> tags (classic wpautop behaviour).
My question is: How can I disable that only for custom blocks? I could use this:
remove_filter( 'the_content', 'wpautop' );
but I don't want to alter the default behaviour.
Some additional findings. The php function use for block rendering use get_the_excerpt(). Once this function is used (and i assume is happening for get_the_content() ) the wpautop filter is applied and the html markup of the block gets messed up.
I don't know if this is a bug or the expected behavior but is there any simple solution to this that does not involve removing the filter ? ( For ex on themeforest removing this filter is not allowed.)
We have by default:
add_filter( 'the_content', 'do_blocks', 9 );
add_filter( 'the_content', 'wpautop' );
add_filter( 'the_excerpt', 'wpautop' );
...
I skimmed through the do_blocks() (src) and if I understand correctly it removes the wpautop filtering if the content contains any blocks, but restores the filter for any subsequent the_content() usage.
I wonder if your render block callback contains any such subsequent usage, as you mentioned a WP_Query loop.
One could e.g. try:
$block_content = '';
remove_filter( 'the_content', 'wpautop' ); // Remove the filter on the content.
remove_filter( 'the_excerpt', 'wpautop' ); // Remove the filter on the excerpt.
... code in callback ...
add_filter( 'the_content', 'wpautop' ); // Restore the filter on the content.
add_filter( 'the_excerpt', 'wpautop' ); // Restore the filter on the excerpt.
return $block_content;
within your my_plugin_render_block_latest_post() callback code.
I'm posting this question as I'm having a little trouble adding the author info to my post hero on my website.
I'm using the Genesis framework with Wordpress, so what I did is removing the post info from the post and adding it back in the post hero. That all works, except that the author name is not being shown anymore as it's not yet fetched in the post loop.
// Remove entry title
remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
// Remove post info
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
// Add page title
add_action( 'hero-info', 'genesis_do_post_title' );
// Add page info
add_action( 'hero-info', 'genesis_post_info', 12 );
To be able to add the post author info back in the post hero, I've looked up stackoverflow and found a link where the OP was able to fix it by creating a shortcode for it and running it in the hero-info
function author_shortcode() {
global $post;
$author_id=$post->post_author;
the_author_meta( 'display_name', $author_id );
}
add_shortcode('author', 'author_shortcode');
This shortcode [author] is then added into
add_filter( 'genesis_post_info', 'custom_post_info' );
function custom_post_info( $post_info ) {
if ( is_archive() || is_home() ) {
$post_info = __( 'Article by [author] [post_author_posts_link] on [post_date] - [post_comments zero="Leave a Comment" one="1 Comment" more="% Comments" hide_if_off="disabled"]', 'tcguy' );
return $post_info;
}
}
This is the result now: http://imgur.com/a/6lX5J
It is shown in the wrong place for some reason. Anybody knows how this can be?
The site can be found here: http://websforlocals.com/business/
Hope I gave enough info, and that someone with the same problem can be helped out.
It is issue in your ShortCode registering php code.
When adding short-code we should not ECHO anything as this way it will not be echoed at the place we want to but at the top of the post content.
So always return the output in short code function, and then echo the short-code function.
Now WordPress have a convention for functions which echo the result and which returns the result, i.e. the_author_meta vs get_the_author_meta (first one which you are using will display/echo the result, however get_ functions will return the values).
We need to use get_the_author_meta instead of the_author_meta in your shortcode registering block and it will solve your displaying location issue.
function author_shortcode() {
global $post;
$author_id=$post->post_author;
return get_the_author_meta( 'display_name', $author_id );
}
add_shortcode('author', 'author_shortcode');
Is there a way where I can get the default output of the_content in WordPress post? Some of the plugin is using add_filter to the content to add their desired result like related post plugins where they add the result at the end of the content of a post. What I want to happen is to get the default formatting functions of WordPress core without any additional filters from other plugins.
You can use remove filter function.
remove_filter( $tag, $function_to_remove, $priority );
For more please refer below link
https://codex.wordpress.org/Function_Reference/remove_filter
You can use the_content filter itself to get the post object or the content , you can modify it or you can unhook it and again hook it as per your requirement .
If I have understood your requirement this link will help you, if you need something different please ask me.
Guys thanks for responding to my question I think I got it somehow but need to run more test. What I did was I replicate the process on how the_content() function of WordPress work. Base from my research there are 10 default filters coming from WordPress core you can see on this link: read here
I just created my own function like the_content() from WordPress and apply the default filters to my own function. Put this code in your functions.php
if(!function_exists(default_post_content)){
function default_post_content( $more_link_text = null, $strip_teaser = false) {
$content = get_the_content( $more_link_text, $strip_teaser );
$content = apply_filters( 'default_post_content', $content );
$content = str_replace( ']]>', ']]>', $content );
echo do_shortcode($content);
}
add_filter( 'default_post_content', array( $wp_embed, 'run_shortcode' ), 8 );
add_filter( 'default_post_content', array( $wp_embed, 'autoembed'), 8 );
add_filter ( 'default_post_content', 'wptexturize');
add_filter ( 'default_post_content', 'convert_smilies');
add_filter ( 'default_post_content', 'convert_chars');
add_filter ( 'default_post_content', 'wpautop');
add_filter ( 'default_post_content', 'shortcode_unautop');
add_filter ( 'default_post_content', 'prepend_attachment');
}
Then for example in your single page template(single.php) instead of using the usual the_content I can use my function default_post_content();. Now I don't need to worry about any of the plugins that create additional data to the_content() function.
I'm having a little trouble modifying the page title for my archive pages. I have a custom post type for movies, and I can't seem to alter it. Right now it reads "Movies archive". I'd like to change it altogether to something like "Programme".
Any ideas?
Thanks!
It depends on the function your template is using to output the title of your custom post type archive.
I guess it's using either wp_title or get_the_archive_title so you can try adding a filter (inside functions.php of your theme):
function movies_archive_title( $title ) {
if(is_post_type_archive('movie'))
return 'Programme';
return $title;
}
add_filter( 'wp_title', 'movies_archive_title' );
add_filter( 'get_the_archive_title', 'movies_archive_title' );
If the page viewed is the movies archive (be sure to replace 'movie' with the exact name of your custom post type name) then it replace the title with Programme.
You can add a filter in functions.php like this to override the title, yet preserve the site name and title separator:
function movies_archive_title( $title ) {
$site_name = get_bloginfo();
$sep = apply_filters( 'document_title_separator', '|' );
$sep = str_pad( $sep, 30, " ", STR_PAD_BOTH );
if(is_post_type_archive('movie'))
return 'Programme'.$sep.$site_name;
return $title;
}
// Raise priority above other plugins/themes that
// have effect on the title with 900 (the default is 10).
add_filter( 'wp_title', 'movies_archive_title', 900 );
add_filter( 'get_the_archive_title', 'movies_archive_title', 900 );
Replace movie with your custom post-type name and Programme with the desired title in the above example.
With some plugins and themes you need to raise the priority with the priority parameter of the add_filter method or you won't see a change.