I'm trying to make a shortcode OUTSIDE POST/PAGE in WordPress.
The problem is, when I'm placing the do_shortcode() function, HTML automatically document the PHP code (e.g <!--*php code* -->).
What can cause the problem?
You don't actually place the do_shortcode() in your pages. Somewhere in your functions.php or shortcodes.php file you set up the function and assign a shortcode to the function.
function myFunction($params = array()) {
//function code here
}
add_shortcode('shortcodename', 'myFunction');
Then once that is done you can put [shortcodename] into your pages, inside square brackets just like that. There are some plugins that will allow you to run php and enter it right on the page, but the default shortcode functionality requires you to add the shortcode first.
Related
So, I have this PHP function that adds the X-UA-Compatible meta tag to the head of every page of my Wordpress site. (I'm new to Wordpress and PHP so forgive me about terminology). It's in the functions.php file of my StudioPress child theme.
The problem is the tag only works if it's placed very near the top, and the way I have it set up now causes the tag to appear halfway down, so it doesn't work. How can I force this tag to appear at the top of the head of each page?
Ex : You can use wp_head action. The wp_head action hook is triggered within the <head></head> section of the user's template by the wp_head() function. Although this is theme-dependent, it is one of the most essential theme hooks, so it is widely supported.
function aaa_custom_function() {
?>
Your Tag
<?php
}
add_action('wp_head', 'aaa_custom_function', 1000);
Use the third parameter to control the position. Change 1000 to another values to find the perfect position.
Create your custom hook like this way.
open header.php then write this below line where you want to show meta tag
<?php do_action("my_custom_metatags");?>
Now open your functions.php and write down this below code
function my_custom_metatags_fn()
{
//your tag goes here.
}
add_action("my_custom_metatags","my_custom_metatags_fn");
//Test this code it will work like a charm for you.
adding to #Damith answer wp_head action will work.
Refrence
also you can override header.php file into your child theme.
add this code in function.php
<?php function your_custom_function() {
// your custom code which you want to add in header file
}
add_action('wp_head', 'your_custom_function', 1);
I have a plugin and I need to correctly include inline css in WordPress using wp_add_inline_style() function. There is not problem to include CSS using this function in plugin, it works fine. But I have shortcodes that generates Custom CSS (different for different shortcode). For example I have shortcode that add text title with custom color to page (I generated unique CSS class name for this title DIV and add corresponding styles).
For example:
[mytitle text="Title example" color="#666666"]
And I have css that I need to inline:
.mytitle-id-4324324 h1 { color:#666666; }
As you see I can have multiple shortcodes on the same page, with different colors inside. The problem that if I use wp_add_inline_style function in shortcode it never not work. It should be inlined in "wp_enqueue_scripts" action, that does not triggered in right time from shortcode as I understand (because shortcodes loaded by WordPress AFTER wp_enqueue_scripts, when WordPress generate content). How to resolve this?
Note: I know that I can add inline CSS in HTML code or echo and it works, but I does not need this. My question how to get this work with wp_enqueue_scripts()?
As I understand I need to get one big global custom CSS from entire page shortcodes somehow, and then send it to some function that will include this big custom css for this page. But how to do this because shortcodes defenitions executed after including CSS by WordPress?
Example code for my shortcode:
function mgt_shortcode_header_block_wp($atts, $sc_content = null) {
.. some code here where shortcode show HTML output and generate custom css
}
add_shortcode("mgt_header_block_wp", "mgt_shortcode_header_block_wp");
How I can add inline custom css from shortcode? I can't do this inside shortcode (because its will not be called in wp_enqueue_scripts action) and I can't add some function after this function, because - custom css variable available only inside shortcode (I can't make it global var, because this is not good to use global vars here), I understand that I may need to pass it to some function from shortcode, something like:
function mgt_shortcode_header_block_wp($atts, $sc_content = null) {
.. some code here where shortcode show HTML output and generate custom css
mgt_add_to_inline_styles($custom_css); // how should work this function to correctly add inline styles passed to it?
}
add_shortcode("mgt_header_block_wp", "mgt_shortcode_header_block_wp");
But this will not work, because mgt_add_to_inline_styles() function will be executed only when this shortcode will be used on page in content, and this will be AFTER wp_enqueue_scripts action in any case, even if I will try to add mgt_add_to_inline_styles() to wp_enqueue_scripts action somewhere somehow.
So from my example I does not understand what code should be inside mgt_add_to_inline_styles() function to make it work correctly?
After digging internet for an answer I cannot find any idea about how to deal with my issue. I think the problem is common for someone who knows PHP a little bit.
To describe the situation. For some custom WordPress plugin I've got two PHP files: ff_config.php and loantest_form.php. First file contains some configurations of plugin plus following lines:
/**--------------------------TABLE SHORTCODES-----------------------*/
function render_loantest_form() {
include(plugin_dir_path(__FILE__) . 'front/loantest_form.php');
}
add_shortcode( 'render_loantest_form' , render_loantest_form );
/**--------------------------DISPLAY PLUGIN IN FOOTER-----------------------*/
add_action('wp_footer', 'display_loantest');
function display_loantest() {
echo render_loantest_form();
}
Which I suppose rendering second file containing enqueue scripts (js/css) and whole HTML output and placing in wp_footer where it exactly is on my page.
The question is: how to change mentioned lines to allow me to place render result (loantest_form.php) in specific div / id on page (for example #sidebar-slider)?
If you want to display your shortcode in a template file,
echo do_shortcode('[render_loantest_form]');
Enable the use of shortcodes in text widgets.
add_filter( 'widget_text', 'do_shortcode' );
And inside the text editor
[render_loantest_form]
You can read more about do_shortcode()
Note that you need to be sure that the file load in the render function is good and that it returns the expect content.
I have written a security function.
I want to call this function in all pages which are created by wordpress back end only.
(Does not want want to called by all php files).
Is there any hook available?
you can use in a php file (like header.php or functions.php) like this
if(is_page())
{
// code to execute on all pages
}
You can tie it to the 'wp_head' hook.
http://codex.wordpress.org/Plugin_API/Action_Reference/wp_head
i've written a plugin which shortcodes can easily be used in every post and page. As this plugin can be useful in a sidebar as well i want to make the text widget usable for my shortcodes.
When i googled this i found out that i can use the add_filter() function to ensure that, but this is only possible if i have access to the theme's functions.php. But as i am the creator of the plugin and not of the theme, this is not usable for me.
Does anybody know how i can make a shortcode which is introduced with a plugin usable in the widgets section?
Thanks!
Open your theme's function file.
Find a free spot after the opening php tag that isn't part of a function.
add this:
if (!is_admin())
{
add_filter('widget_text', 'do_shortcode', 11);
}
save the file and you should be all set.
Open your page in edit mode.
Select your page location and line where you want to add short code.
Add code here and update..