Closing shortcode in WordPress - wordpress

I made a simple WordPress plugin which highlights text.
add_shortcode('close-span', 'highlighter_closing_span_shortcode');
function highlighter_closing_span_shortcode($atts) {
return '</span>';
}
It's the closing shortcode part of the plugin. In this case, users must type "[close-span]". I want to change it to "[/span]". How can I modify the code above?

You can use the $content parameter of the shortcode to allow users to put copy between tags:
add_shortcode( 'span', 'my_span_shortcode' );
function my_span_shortcode( $atts, $content = null ){
return '<span class="highlighted">' . $content . '</span>';
}
You would use the shortcode like this:
[span]This will be highlighted[/span]
and that would result in:
<span class="highlighted">This will be highlighted</span>

Hi #Peter (would comment if I could) this may go without saying but have you tried
add_shortcode('/span', 'highlighter_closing_span_shortcode');
function highlighter_closing_span_shortcode($atts) {
return '</span>';
}

Related

How to build custom plugin to integrate with current theme

I am developing a plugin with a CPT and a CPT template. I would like the template to be able to integrate into any theme it is used with. I thought of calling the 'the_content' hook from my template but I have no idea how to do that as all I can find is calling it from the functions.php file. Please can you tell me how to code this in the CPT template file (like single-CPT.php) or maybe I am heading in the wrong direction so please redirect me. Thank you!
EDIT:
If you only want to add content to the_content function you can do something like that:
add_filter('the_content', "test_the_content");
function test_the_content($content){
if(is_singular('test')){
$content = $content.test_additional_content();
}
return $content;
}
function test_additional_content(){
ob_start(); ?>
<div class="test">
Here what you want to display after the_content
</div>
<?php
return ob_get_clean();
}
ORIGINAL ANSWER
If I understood your question correctly, here is how to define the templates of your custom post type in your plugin. This way it will also be possible to overwrite the plugin template from the theme.
The example below is for a CPT called "test", so you have to adapt the code according to the name of your CPT.
add_filter('template_include', 'my_plugin_templates');
function my_plugin_templates($template) {
$post_types = array('test');
if (is_post_type_archive($post_types) && !file_exists(get_stylesheet_directory() . '/archive-test.php')) {
$template = plugin_dir_path( __FILE__ ) . 'archive-test.php';
}
if (is_singular($post_types) && !file_exists(get_stylesheet_directory() . '/single-test.php')) {
$template = plugin_dir_path(__FILE__) . 'single-test.php';
}
return $template;
}
You can also take a look to this repo: https://github.com/zecka/cpt-in-plugin

Wordpress custom button link output after the_content

I'm trying to insert a button to show at the end of each post on Wordpress in which the link it goes to is defined by a setup using the custom fields plugin. When creating each post, I am able to select the link I wish to display.
Here is the code I have which I know is wrong but I was hoping someone could help here.
function wpb_after_post_content($content){
if (is_single()) {
$content .= 'Contact Franchise →';
}
return $content;
}
add_filter( "the_content", "wpb_after_post_content" );
I assume $franchise_profile_url is a variable and you should concatenate it in the string like this
$content .= 'Contact Franchise →';
function afterContent($content) {
if(!is_feed() && !is_home()) {
$content.= "<div class='footNote'>";
$content.= "<h4>Click the below button if you like it:</h4>";
$content.= "<p><a href='#'>LIKE</a></p>";
$content.= "</div>";
}
return $content;
}
add_filter ('the_content', 'afterContent');
Use the above function. It will help you to achieve what you need.
Thanks for the help here, however, that code simply links back to the post itself and isn't pulling in the URL as set on the post using custom fields. This is the code I had set up before which was working on a default post setup but now I wish to use an alternative method in the functions.php file
<?php if( get_field('franchise_profile_url') ): ?>
Contact Franchise →
<?php endif; ?>

When to use add_action vs add_filter?

It seems cleaner to make a plugin and use add_action -- why would you use add_filter?
add_filter hook is mainly used to change the content before displaying it ..
Suppose I want to print the title as bold when I use the_title() function of WordPress. So to modify this function only in terms of display, we can use add_filters.
Example : add_filter( 'the_title', function( $title ) { return '<b>' . $title . '</b>'; } );

Trying to insert a linked featured image into the content

I am using filter hook to insert a featured image into the_content, that works, but when I try to wrap the featured image in an anchor tag, the link tag ends up directly after the content(not wrapping the image at all)
Is there something I am missing as far as understanding how to filter the_content()? Here's my code:
add_filter('the_content', 'add_img_to_ps_archive');
function add_img_to_ps_archive($content) {
if (is_post_type_archive('past_symposia') ) {
echo $content . '<a href ="#" "alignleft">' . the_post_thumbnail('symposia-thumb') .
'</a>';
} elseif( is_singular('past_symposia') ) {
echo $content . '<br />';
} else {
return $content;
}
}
Try to use commas ',' and not dots '.' for concatenating - don´t ask me why, but wordpress does that sometimes...
This happened because the_post_thumbnail() outputs the image tag directly to the output buffer. You need to use get_the_post_thumbnail() to return the image tag so you can concatenate it with $content.

Do I have to add_filter() before apply_filters() in Wordpress?

I'm trying to understand Wordpress plugin like:
apply_filters( 'gettext', $translations->translate( $text ), $text, $domain );
I'm looking for all codes in Wordpress, I can't find:
add_filter( 'gettext', ....);
Why there is no add_filter for this plugin? Or I missed something? Same thing like:
do_action('wp_loaded');
I can't find:
add_action('wp_loaded', ....);
apply_filters is like, 'if there are any filters with this name, run the attached callbacks with these parameters'. So if there is no add_filter for that name, it means that there is no filter that's going to be run with the apply_filters call at the moment.
The same goes with do_action and add_action.
I am a beginner in PHP - WordPress stack as well, but this is from my understanding.
The plugins call apply_filters without having any add_filter in their codes is to allow the website users to add custom logic to their plugins. We - the users, can add our own function and use add_filter to register our functions.
For example, this piece of code is from the plugin. Normally, it shows all products but it provides us a way to not show a specific product.
// Plugin's
if (apply_filters( 'plugin_show_products', true, $product->get_id() ) ) {
$this->show_products();
}
So, if we - the users, want to customize a bit. We can add our own function as following (maybe in functions.php)
// Our custom changes
function my_own_changes($boolean, $product_id) {
if ( $product_id === 5 ) return false;
return true;
}
add_filter( 'plugin_show_products', 'my_own_changes', 10, 2 );
This translates to: The plugin will behave normally but for my own site, it will not show the product with ID of 5!
I have come across this type of code in a plugin or theme where the apply_filter is used without necessarily having an existing filter or add_filter
In this case, where the apply_filters is used without a filter you will have to call the function again where you want to run it. For example, in the header of a theme.
The following is an example of apply filters used in a function that is again called in the header.php
if ( ! function_exists( 'header_apply_filter_test' ) ) {
function header_apply_filter_test() {
$filter_this_content = "Example of content to filter";
ob_start();
echo $filter_this_content;
$output = ob_get_clean();
echo apply_filters( 'header_apply_filter_test', $output );//used here
}
}
Now in the header.php file, you would have to call this function since it is not hooked anywhere. So, in this case, to display the output in the header you would call the function like this :
<?php header_apply_filter_test(); ?>
You could as well write this code with a hook and it would do the same thing i.e display the output in the header.
add_filter('wp_head', 'header_apply_filter_test');
if ( ! function_exists( 'header_apply_filter_test' ) ) {
function header_apply_filter_test() {
$filter_this_content = "Example of content to filter";
ob_start();
echo $filter_this_content;
$output = ob_get_clean();
echo $output;
}
}
For this second option, you would still have the capability of using apply_filters anywhere else to call the callback function header_apply_filter_test() since the filter now exists.
So the bottom line in my view is a use case since either approach works!

Resources