How to include a Wordpress Shortcode in your Code, not in the Posts Section - wordpress

I want to insert a hardcoded short code in my code, and not from the usual Text Editor we usually use.
Basically I want this to add a gallery, and the user doesn't need to change the shortcode from the CMS so I will be hardcoding this.
How would I need to do this, I tried to just post it in my .php file but it doesn't work.
This is the code I want to add:
[jj-ngg-jquery-slider gallery="1" width="866" height="341" ]

This will do the trick to include in .php files:
<?php echo do_shortcode('[jj-ngg-jquery-slider gallery="1" width="866" height="341"]'); ?>

shortcodes were created to include in post or pages. I could be wrong but wordpress checks the input of a post and if it finds a shortcode it will replace it with the html. I don't think it will work if you add shortcodes in your .php file because wordpress doesn't look for shortcodes in your php files
You could just create a function in functions.php to generate the html you need. Then you just call that function within your theme .php file. That's how most plugins are made. Shortcode for post & pages and function in the php files.
example:
<?php echo myGallery(array('gallery'=>1, 'width'=>866, 'height' => 341); ?>

Did you try this method ? do_shortcode($content)
I've seen it on http://codex.wordpress.org/Shortcode_API

Related

Add Shortcode to All Post Types

I want to display shortcodes (I'm using fruitful Shortcode) above the post title tag (H1). And I want to display it to all my post (also as a template).
i want to be like this
Thanks.
If you want to adjust the way your single posts are displayed for all the posts, you have to edit the single.php file in your theme folder. You can do this in the backend under "Design" -> "Theme Editor" or you can access the folder and files via FTP.
For your default posts in wordpress you just need to open the single.php file and look for the h1 with your title. It can look something like:
<h1><?php the_title(); ?></h1>
Above that (if you want to display it above), you can put your shortcode with the do_shortcode() function:
<?php echo do_shortcode('[name_of_shortcode]'); ?>
If you are not using normal posts of wordpress but custom post types or something else, please have a look at the wordpress template hierarchy to find out, which file to edit or how to name the new files for creating template files: https://developer.wordpress.org/themes/basics/template-hierarchy/
If you are using a theme that is not made by you, you should make sure you keep your theme updateable. So if you overwrite the single.php, with the next update of your theme, the file will not have your edits. You need a child theme, if you want to keep your parent theme up to date but also customize it in the page templates. Here is a nice tutorial for child themes: https://www.smashingmagazine.com/2016/01/create-customize-wordpress-child-theme/

Is there some way to insert custom archive loop to any theme from plugin?

I'm coding plugin that create custom post type with custom fields. Now I need to build custom archive/category/tag templates which should contain custom fields.
The problem is that I couldn't insert template part inside archive loop from plugin.
Is there some hook to insert custom loop inside activated theme? Something like this:
add_filter('the_content', 'change_content_in_single_post');
Now I'm using this hook:
add_filter( 'template_include', 'change_whole_cpt_archive_template' );
... but when I use template_include hook it changes whole template and I need to do something like the_content filter. That hook get template of current theme and replace standart content template with my custom template - make it part of activated theme. It makes my CPT single page template compatible with any wp theme. Now I need to replace standart archive loop with my custom loop. For example: on archive page show posts without images... It must be something that replace result of standard get_template_part() function with custom template from plugin. That's what I`m searching for...
Anyway, maybe you know some better ways to make plugin (archive template) compatible with wp themes?
Huge thanks for any help!
you need to introduce some logic to only run on your cpt archive page.
add_filter( 'template_include', 'change_all_archive_template' );
function change_all_archive_template($template){
if ( is_post_type_archive('cptname') ) {
$template= 'find the template!';
//if( file_exists ) --> look in theme 1st??
//else --> load from plugin..
}
return $template;
}
There are quite a few plugins that use this technique to look in the active theme 1st and if not found, find the file in your plugin.
I found solution in woocommerce plugin files.
So... the best way to build custom archive template from plugin is to use template_include hook and in custom template file set header and footer from activated theme:
<?php get_header();
// custom archive loop
get_footer(); ?>
To make it more compatible with any wp themes use stanard wordpress and bootstrap classes.

How to use wordpress shortcode in template

I need help with wordpress shortcode. I have written shortcode function in functions.php in my theme files. Now I create page in wordpress administration and then I need to place this shortcode into special place in my theme but with content which I wrote in administration.
I hope that you understand what I mean.
Thank you for your help.
In your template code:
<?php echo do_shortcode('[your-shortcode]'); ?>
Replace [your-shortcode] with whatever your shortcode is, including parameters/attributes and/or closing tags if required.

Wordpress plugin shortscodes in other plugins?

I'm developing a plugin that creates custom wordpress pages, however I ran into a problem when trying to get shortcodes from other plugins to integrate into my plugin, well more like I couldn't find any info on it.
For example if someone uses my custom page builder and adds a short code [gallery] that is associated with an enabled plugin, I want the shortcode to do it's thing.
Could anyone point me in the right direction for this?
Try wordpress
http://wordpress.org/plugins/synved-shortcodes/
while this plugin let's you integrate shortcodes on every wordpress page , I suppose that you issue is with wordpress functions.php file where you can have a look at , with this plugin installed !
You can check if a shortcode exist before printing it with the function shortcode_exists() (https://codex.wordpress.org/Function_Reference/shortcode_exists)
Example:
<?php
if ( shortcode_exists( 'gallery' ) ) {
// The [gallery] short code exists.
}
?>
Then, if it exist and you want to print that shortcode with PHP code you should use do_shortcode() function (https://developer.wordpress.org/reference/functions/do_shortcode/)
Example:
<?php echo do_shortcode('[gallery]); ?>
Always use in WordPress's in-built
the_content
hook to print your custom page content.
WordPress by default process all shortcodes before outputting any stuff from this hook.
More info can be found here: https://codex.wordpress.org/Function_Reference/the_content

categories in Wordpress sidebar.php

the sidebar.php shows
<li>
<?php wp_list_categories('show_count=1&title_li=<h2>Categories</h2>'); ?>
</li>
so which php file generates the Categories in the sidebar (wrapped in a tags and with number of posts)?
If you're trying to change the output of this function, you can do it with a custom theme filter. Add the following to your theme's functions.php:
function custom_wp_list_categories($categories){
// do something to the $categories returned by wp_list_categories()
return $categories;
}
add_filter('wp_list_categories', 'custom_wp_list_categories');
The benefit of this approach is that it means that if you upgrade WordPress, you don't have to worry about making your changes again to the core files.
The function is located inside wp-includes/category-template.php
You can find out where any function is located by looking at the WordPress codex - at the bottom of each page, there is a link to where the function located.
Documentation for wp_list_categores
wp_list_categories function source code
Why edit the core when there are so many options to choose from!
http://codex.wordpress.org/Template_Tags/wp_list_categories

Resources