Wordpress custom button link output after the_content - wordpress

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; ?>

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

Elementor page builder shortcode issue

I'm facing a weird problem when using the Elementor Wordpress Page Builder.
After creating a custom shortcode and inserting it into any page position, it also shows up at the top of the page, but only in Edit mode.
Top of the page:
Place where I want to insert shortcode:
This answer on an unrelated site helped me solve this Elementor issue. https://wp-types.com/forums/topic/shortcode-output-showing-up-in-the-wrong-place/
I just had to include ob_start(); and $content = ob_get_clean(); return $content; in my function. Here is what it looks like:
function custom_author_link_function() {
ob_start();
coauthors_posts_links();
$content = ob_get_clean();
return $content;
}
add_shortcode('custom_author_link', 'custom_author_link_function');
This is my working example:
function name_it( ){
ob_start();
function_name();
$content = ob_get_clean();
return $content;
return function_name();
}
add_shortcode( 'shortcode_name', 'name_it' );
Just look at function_name(); and return function_name(); lines to avoid errors.

Replace shortcode with a link

First sorry for the improper question format. Not sure how to write this.
My problem:
I've created a shortcode which fetches content from a post id and renders the post content in a text editor in the Divi theme (Elegant Themes - Divi). Now Divi releases a new Visual Builder which does not output the shortcode as it cannot parse this when the Visual Builder is enabled. But it shows when it's not enabled.
My Shortcode
function fetch_content_shortcode() {
global $post;
$id = "987453719"; // my post ID
ob_start();
$output = apply_filters('the_content', get_post_field('post_content', $id));
$output .= ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode('divi_content', 'fetch_content_shortcode');
Now I want to replace/convert the entire [divi_content] into a link which will take them to that custom post type post (post id 987453719). How I can achieve this.
Thanks and sorry for the bad explanation.
function fetch_content_shortcode( ) {
$link = get_the_permalink(get_the_ID());
return 'Click';
}
add_shortcode( 'divi_content', 'fetch_content_shortcode' );

Closing shortcode in 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>';
}

social icon location in wordpress

function flx_social_sharing_buttons($content) {
// Show this on post and page only. Add filter is_home() for home page
if(is_singular()){
// Get current page URL
$shortURL = get_permalink();
// Get current page title
$shortTitle = get_the_title();`enter code here`
// Construct sharing URL without using any script
$twitterURL = 'https://twitter.com/intent/tweet?text='.$shortTitle.'&url='.$shortURL.'&via=flx';
$facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$shortURL;
$googleURL = 'https://plus.google.com/share?url='.$shortURL;
$tumblrURL = 'http://www.tumblr.com/share/link?url='.$shortURL;
// Add sharing button at the end of page/page content
$content .= '<div class="flx-social">';
$content .= '<a class="flx-link flx-facebook" href="'.$facebookURL.'" target="_blank"><i class="fa fa-facebook"></i></a>';
$content .= '<a class="flx-link flx-twitter" href="'. $twitterURL .'" target="_blank"><i class="fa fa-twitter"></i></a>';
$content .= '<a class="flx-link flx-googleplus" href="'.$googleURL.'" target="_blank"><i class="fa fa-google-plus"></i></a>';
$content .= '<a class="flx-link flx-tumblr" href="'.$tumblrURL.'" target="_blank"><i class="fa fa-tumblr"></i></a>';
$content .= '</div>';
return $content;
}else{
// if not post/page then don't include sharing button
return $content;
}
};
add_filter( 'the_content', 'flx_social_sharing_buttons');
i use this code to display sociAl sharing buttons below my post ,
i need to add shortcode functionality so that it appears wherever i want
how to do it pls help
You simply can do it with bellow code,
<?php echo do_shortcode('[the_content]'); ?>
just pest this code where you want social icons and u can get it on those place.
I'm not sure that I understand your question correctly, but hopefully you'll be able to use the following to address your challenge.
If I wanted to repurpose the function to render the social sharing buttons, I would start of by changed the first line to
function flx_social_sharing_buttons($content = false) {
The code should at this stage still, as expected, add the social sharing buttons after each post. To add it programmatically or within a post or page you can add the following line, creating a shortcode:
// Add the [my-social-buttons] shortcode
add_shortcode( 'my-social-buttons', 'flx_social_sharing_buttons' );
Documentation available at: https://codex.wordpress.org/Shortcode_API
Great resource for creating shortcodes and other Wordpress features:
http://generatewp.com
You can now use the shortcode in a post or page. To use it inside a php file, do the following
echo do_shortcode('[my-social-buttons]');
If you find that you'd rather now use your function programatically all that's left to do is to remove the original hook, if your social sharing buttons are now duplicated:
// Remove or comment out
// add_filter( 'the_content', 'flx_social_sharing_buttons');

Resources