I am creating new shortcodes for Wordpress on my local version of a Wordpress website.
In functions.php, I am adding for example:
function shortTest() {
return 'Test for shortcodes ';
}
add_shortcode('shortTestHTML', 'shortTest');
Adding the function only is OK, but when I add the add_shortcode() portion, I get a major issue.
It breaks something somehow and I get 500 errors, meaning I can't even load my website locally anymore.
Any thoughts???
Thanks so much!
EDIT:
From PHP Error Log:
[21-Jun-2011 19:02:37] PHP Fatal error: Call to undefined function add_shortcode() in /Users/jonas/Sites/jll/wp-includes/functions.php on line 4505
1) Make sure that you have included shortcodes.php file somewhere (for example, in wp-load.php or whatever other more appropriate place may be).
require_once( ABSPATH . '/wp-includes/shortcodes.php' );
2) Make sure that this file does exist on your installation (which I think you have otherwise we would see a different error).
3) Where do you call this function from (I see it is called from functions.php, but which place)? Possible problem here -- functions.php is loaded prior to shortcodes.php, and if you do use that add_shortcode function before shortcodes.php is loaded you most likely will see this error. Double check your code in that place -- maybe move the call to add_shortcode to another place.
mentioned functions.php is not in wp-include/ directory.
it is in: wp-content/themes/<your-theme-name>/functions.php
adding this in functions.php you say? Well I don't know about that, the way I did it was create a folder inside the wp-content/plugins folder, e.g. shortcodetest.
Inside this folder create a shortcodetest.php file.
in that file you basically write your code:
<?php
/*
Plugin Name: ShortCodeTest
Plugin URI: http://www.example.net
Description: Print a test message
Version: 0.1
Author: anonymous
Author URI: http://www.example.net
*/
add_shortcode('shortcodetest', 'shortcodetest_function');
function shortcodetest_function() {
return "test of shortcode";
}
?>
Then you login as admin, you will see a plugin ShortCodeTest, activate it. Then you can use the shortcode in your posts.
Note that the comments are important... they show up in the plugin description.
I got a way to execute them but it's a little triky :)
You need to change a little bit your template by putting your shortcode (for example: [inline ....]) between <shortcode></shortcode> then
Here is the function to place at the end of your function.php.
function parse_execute_and_echo_shortcode($_path) {
$str = file_get_contents($_path);
while (strrpos($str, "<shortcode>")) {
$beginShortcode = strrpos($str, "<shortcode>");
$endShortcode = strrpos($str, "</shortcode>");
$shortcode = substr($str, $beginShortcode + 11, $endShortcode - ($beginShortcode+11));
$shortcode = do_shortcode($shortcode);
$str = substr_replace($str, $shortcode, $beginShortcode, $endShortcode + 12);
}
echo $str;
}
Then you can call function parse_execute_and_echo_shortcode and giving it the path to your file containing the shortcodes.
Hope that can help someone
Check your error.log file (it should be in your apache log folder).
It probably has to do with add_shortcode not existing as a function.
Put your shortcode in the file /wp-includes/shortcodes.php this way you make sure it will be loaded when all the blows and whistles are up and running.
Related
My site is http://www.empoweryourfamily.org/wordpress/
I was working on a plugin and wrote the following code in functions.php
add_action( 'wp_enqueue_scripts', 'table' );
function table() {
wp_register_script('table1', plugins_url() . '/Webinar_Reg/table.js', false, null, true);
if(is_admin()){
wp_enqueue_script('table1');
}
}
But, the moment I clicked on update everything went blank and now neither the frontend nor the backend is opening.
You most likely have a syntax error in functions.php or the function table() is already defined.
Try rolling back the changes you just applied.
Otherwise try looking in the server log for a specific error.
Using table as a function name is bad practice. You should name a function in such a way that it will be completely unique. Use something like mypluginname_table or mytheme_table where mypluginname is the name of your plugin and mytheme the name of your theme. And remember, function names needs to be unique, no two functions can have the same name, except if one is wrapped in a if(!function_exists()) {} conditional statement
I just replaced the modified functions.php file with the old(backup file) file and it is opening properly now. Thanks for your answers.
I've been having this issue for a while but keep just working around it an thought I'd finally get it solved.
I'm trying to include files into my main plugin document (the one that has the plugin title and version in it) like this:
define('SBT_PLUGIN_URL', plugin_dir_url(__FILE__));
include(SBT_PLUGIN_URL . 'competition_table.php');
inside the competition_table.php is an add_shortcode(); function that needs to run, in order for the shortcode to be registered with wordpress:
function add_table() {
//Run code here
}
add_shortcode('competition_table', 'add_table');
When I run the code on the site the link resolves properly, including the correct file, however I get this Fatal Error:
Call to undefined function add_shortcode()
However if I add exactly the same code that is in the competition_table.php into my main plugin document then the code runs perfectly.
So basically, my question is, why is Wordpress not recognizing it's own function and how can I include the file to make the code run properly?
Thanks in advance
You have to develop with WP_DEBUG enabled. It dumps an error: wrapper is disabled in the server configuration. That lead me to this: "Trust me, you do not want to include from URLs.".
Then I realized you're defining that constant with plugin_dir_url(), when what you need is a path. The following magic constant does the job:
include_once __DIR__ . '/competition_table.php';
Thanks to the feedback from #b__ I have managed to solve this issue.
For some reason, Magic Constants don't always work with wordpress, however, you can use it's equivalent to get the same effect:
include_once dirname(__FILE__) . '/competition_table.php';
When including files for use in a wordpress plugin you should always include via a PATH, not by a URL.
I am having trouble getting wp_enqueue functions to work. I've looked at all the documentation on it but am having trouble sifting through and finding out what is supposed to go where.
so far I understand that I am supposed to register and enqueue the files from the functions.php file of the theme I am creating. So that is exactly what I do. I create some PHP tags and drop it in the middle of them, at the bottom of the page. Save and Upload.
When I reload, it just returns a blank white screen, must be an error in the code or something.
Here is the function:
<?php
function add_scripts(){
wp_register_script('jquery', 'http://code.jquery.com/jquery-1.5.2.min.js');
wp_register_script('nivo', get_bloginfo('url').'/scripts/nivo.js');
wp_register_script('slimbox',get_bloginfo('url').'/scripts/slimbox2.js');
wp_register_script('picasa', get_bloginfo('url').'/scripts/jquery.EmbedPicasaGallery.js');
wp_register_script('pwi',get_bloginfo('url').'/jquery.pwi-min.js');
wp_register_script('swf', get_bloginfo('url').'/jquery.swfobject.1-1-1.min.js');
wp_register_script('simpletube',get_bloginfo('url').'/scripts/jquery.simpletube.js');
wp_register_script('jqvalidate', get_bloginfo('url').'/jquery.jqvalidate.js');
wp_enqueue_script('jquery');
wp_enqueue_script('nivo');
wp_enqueue_script('slimbox');
wp_enqueue_script('picasa');
wp_enqueue_script('pwi')
wp_enqueue_script('swf');
wp_enqueue_script('simpletube')
wp_enqueue_script('jqvalidate');
}
add_action('init','add_scripts');
?>
So is there some sort of problem with my syntax? I'm not that strong with PHP.
Any help is greatly appreciated. Thanks!
It's kind of hard to debug it without seeing the whole file but the fact you get a 'blank page' suggests there's definitely something larger than a syntax problem somewhere.
Do you definitely have correctly nested php tags? i.e.
<?php
some code
<?php
some more code
?>
some more code
?>
will give you problems.
Also, it's now common practice to leave the last ?> from the end of the file (it means you wont have any issues with having whitespace after the closing tags and they're not necessary)
On top of that, you've used wp_register_script('jquery'...) - WordPress already has jquery registered. If you wish to re-register it, you need to wp_deregister_script('jquery') first. I'd also only do that outside of the admin, so:
if(!is_admin()){wp_deregister_script('jquery'); <your wp_register_script stuff> }
If these things don't help, copy and paste your entire functions.php file (use pastebin.com and give us a link)
As an aside, you're using get_bloginfo('url') several times - which means you're running lots of unnecessary calls to the database. Stick it into a variable and save yourself a little overhead:
$my_url = get_bloginfo('wpurl');
wp_register_script('thing', $my_url.'/script/location/file.js');
Oh! One more thing, I don't think url is an allowed argument for get_bloginfo() I think you want wpurl
Codex page on get_bloginfo() function
Good luck!
Missing ; for the following two lines:
wp_enqueue_script('pwi')
wp_enqueue_script('simpletube')
Instead of your code I would use:
<?php
function add_scripts(){
wp_enqueue_script('jquery', 'http://code.jquery.com/jquery-1.5.2.min.js');
wp_enqueue_script('nivo', get_bloginfo('url').'/scripts/nivo.js');
wp_enqueue_script('slimbox',get_bloginfo('url').'/scripts/slimbox2.js');
wp_enqueue_script('picasa', get_bloginfo('url').'/scripts/jquery.EmbedPicasaGallery.js');
wp_enqueue_script('pwi',get_bloginfo('url').'/jquery.pwi-min.js');
wp_enqueue_script('swf', get_bloginfo('url').'/jquery.swfobject.1-1-1.min.js');
wp_enqueue_script('simpletube',get_bloginfo('url').'/scripts/jquery.simpletube.js');
wp_enqueue_script('jqvalidate', get_bloginfo('url').'/jquery.jqvalidate.js');
}
add_action('wp_enqueue_scripts', 'add_scripts');
So please notice I have removed "wp_register_script" as using that is totally unnecessary if you are going to call wp_enqueue immediately after register.
wp_register_script
Is used so that you can afterwards call it ANYWHERE else in code without including the path.
Also big change is that I'm not calling the function from
init
But I'm calling it from
wp_enqueue_scripts
Also please consider adding additional parameters to your wp_enqueue_script such as
wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )
I am pulling my hair out. I have created some simple functions that generate random numbers, pulled from a database that I want to use on wordpress pages. (And then call them from the theme files, such as header.php or page.php.)
I have tried putting the functions inside functions.php that is in the theme (as per the documentation I have read), but I keep getting the "call to undefined function" errors! What in the world am I doing wrong?
Example, here is a function inside the functions.php
function randomPollNumber() {
///this gets a random active poll number
$sql12 = "SELECT id FROM poll WHERE removed = 0 AND active = 1 ORDER BY RAND() LIMIT 1";
$result12 = mysql_query($sql12);
$myrow12 = mysql_fetch_row($result12);
$pollquestionid = $myrow12[0];
return $pollquestionid;
}
And I am calling it, from the header.php file with this
<?php echo randomPollNumber(); ?>
And yes, I DID try using the if_function_exists, but of course it cannot FIND the function, so of course it does not exist. Please help?
Very strange - some debugging tips:
Put die('functions.php file loaded') statement at the beginning of functions.php (not inside a function). If it doesn't die, you know the file isn't being loaded.
If it dies, then check your spelling (copy and paste the function name from functions.php into your echo [...]). It's amazing how many times I'm SURE I've spelt it right, when in fact I haven't.
If it doesn't die, check that your file is definitely called functions.php, that it's definitely inside the right theme folder for the theme you are coding.
It's possible that the functions.php file has an error in it, and so is not being parsed, hence Wordpress can't find the function. Check your logs for errors. Load the functions file and nothing else, and check that the function is working. Are you using PHP Unit or something like that?
I have installed a plugin wp-fb-autoconnect
I have activated it in the dasboard. Now I would to use a function from it.
In my theme folder, in one of my templates I want to add FB connect button
if( function_exists(‘jfb_output_facebook_btn’) )
{
jfb_output_facebook_btn();
jfb_output_facebook_init();
jfb_output_facebook_callback();
}
function_exists(‘jfb_output_facebook_btn’) returns false. The function is located in main.php in the plugin folder and i can see it. What's wrong? Please help.
UPDATE
The problem in general. A plugin has declared a function. I need to get access to that function from my theme folder.
‘jfb_output_facebook_btn’ uses formatted quotes. Did you copy/paste that snippet from a blog? Try:
if( function_exists('jfb_output_facebook_btn') )
{
jfb_output_facebook_btn();
jfb_output_facebook_init();
jfb_output_facebook_callback();
}
Be very careful of the kind of quotation marks you pass to PHP!
(BTW: Awesome user name. That game is brutal difficult.)