Elementor page builder shortcode issue - wordpress

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.

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

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

Wordpress: admin_footer_text text not appended

I'm trying to use admin_footer_text in my plugin to add html into admin page. it works. but I've another plugin install that's using the same hook admin_footer_text. my plugin is overwriting the html code of previous one. I want it to append not to overwrite.
here is how I'm using the code
add_filter('admin_footer_text', 'my_footer_admin', 1);
function my_footer_admin($default){
ob_start();
echo 'some text';
$content = ob_get_clean();
return $content;
}
You need to do it like the following example
add_filter('admin_footer_text', 'my_footer_admin', 1);
function my_footer_admin( $default ) {
ob_start();
echo 'some text';
$content = ob_get_clean();
return $default $content;
}

WordPress: Filter Priority not working

I am trying to add attachment gallery after the_content() but it is not moving to below the_content() but top of the_content()
I have set priority to 9999 and still not working. Any idea or suggestion please?
add_filter('the_content', 'admin_gallery_after_content', 9999);
function admin_gallery_after_content($content)
{
$admin_gallery = new q2a_rm_front;
$content .= $admin_gallery->rm_admin_gallery();
return $content;
}

Resources