Replace shortcode with a link - wordpress

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' );

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

How to echo something after header tag (wordpress)?

I want to echo something after header tag in single pages.
I have a filter to add something before content.
function add_custom_meta_to_content($content) {
$queried_object = get_queried_object();
if ( $queried_object ) {
$post_id = $queried_object->ID;
}
$text = get_post_meta($post_id, 'textbox_wporg_meta_key', true);
if( is_single() ) {
$content = $text . '' . $content;
}
return $content;
}
add_filter( 'the_content','add_custom_meta_to_content' );
But Now I want add some data exactly after header tag in single pages by my plugin.
I don't want to edit theme codes.
How can I do that?
You wouldn't normally do this with the the_content filter. Almost all commercial themes would have a specific hook to insert additional code after the post title and before the main content. For instance, the Generatepress framework would enable you to hook into the generate_after_entry_title action hook and you would output additional code there.

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.

Get post id inside shortcode used in loop

I feel like I'm missing something here. So I'm using visual composer masonry grid to show posts. I'm trying to create a shortcode to use in the Visual composer grid builder that shows the date timestamp but even though the shortcode is being used in a loop I can't get the post id or things like the_title or the_date in the shortcode. I can even use other shortcodes within this one to pull in the title and other meta info and it shows post specific info.
This is my attempt... it outputs nothing, no errors at least, but no result...
function lmi_features_time_ago_shortcode( $atts, $post ) {
global $post;
$output = '';
$timestamp = get_the_date('Y-m-d g:i:s', $post->id);
// $timestamp = time_elapsed_string($timestamp);
$output .= $timestamp;
return $output;
}
add_shortcode( 'social_feed_ago', 'lmi_features_time_ago_shortcode' );
Do a var_dump($post) and check if it's actually the post object. If the loop is set to return ids only then the $post variable will be the ID itself.

Resources