Wordpress shortcode does not work - wordpress

I'm trying to create a simple shortcode function that would work in a link like this:
href="http://www.website.com">[short_code]
Please note, I had to remove the a tag because it wouldn't show. I want to be able to change the anchor text of a link with a single edit across multiple pages/posts.
to accomplish this, this is what I've done:
I've added include 'myshortcodes.php'; to functions.php file and created a file called myshortcodes.php
within that file I have the following code:
<?php
add_shortcode('hw', 'hello');
function hello() {
return 'Click Here For More Info';
}
Now when I put [hw] in href="http://www.website.com">[hw] it does work on my local wp install, however, when I do the same on my live server, it doesn't work, no matter what I do. It returns [hw] instead of 'Click Here For More Info'. Any ideas why? Thanks!

Maybe problems with php interpreter on server side.
Check if your server side doing include right.
Below and above your shortcode function place echo with something, and if it will echoes on your site, then we need to dig deeper.
And if it will not echoes, than the problem is in include function.

Related

How to set a variable on functions.php only if the viewed page is the homepage

i am trying to run some PHP code on functions.php to clean some CSSs and add another ones. For now, for testing purpuse i am passing a parameter on the URL and if the parameter is present the code run else, the web load without the PHP code. I need to do a lot of tests on every URL of the site before leave the code running on all the web.
For now, the code is working only for homepage and I want to leave the code running, when i optimize another page i will to add it, etc etc, but I can't use the is_front_page() or is_page() function on functions.php, so what can I do?
I tried adding add_action but this is running after my PHP code, is there a way to use the is_front_page() function on functions.php or i need to change the logic of my code?
Note: I am a developer but I don't know enough about the Wordpress core and how to code in WP.
Note2: I read a lot of questions here and none of them help me.
I don't know your code, but maybe it's ok for you to write your code in a function in functions.php and use that function in page.php template?
How does your add_action code looks?
This should work too:
add_action('get_header', function() {
if ( is_front_page() ) {
// do it
}
});

How to properly use CF7 hooks

I'm fairly new to Wordpress development so please excuse if my question is dumb.
I'm trying to call a REST API when CF7 is submitted. So I've tried to hook into both wpcf7_before_send_mail and wpcf7_mail_sent but to no avail. Whenever I put this code in my theme's functions.php file; the form doesn't submit and keeps on loading (like it's trying to submit)
For simplicity's sake, I tried to do a simple redirect as you see in the following code but that's not working either. The form works when I remove the following code from functions.php file.
add_action("wpcf7_mail_sent", "wpcf7_do_something_else");
function wpcf7_do_something_else($cf7) {
wp_redirct('https://google.com');
}
Please advise what I am doing wrong. Thank you very much!
It keeps loading because it's an ajax function and you are trying to use 'wp_redirect' in it and that won't work.
The 'wpcf7_mail_sent' hook is working fine, you can test that by using something like that
add_action("wpcf7_mail_sent", "wpcf7_do_something_else");
function wpcf7_do_something_else($cf7) {
var_dump($cf7);exit;
}
And while sending the form monitor the 'network' tab in the dev tools of your browser to see the results and you will find that it returns an object of the submitted data.

Fire an action right after Appearance > Theme Options has been saved

I just started working with Wordpress (v. 3.6.1).
I have OptionTree installed and as it seems it handles the Theme Options page. I want to run my function (in a plugin or wherever else) right after the user saves the changes of this page.
So far I found out that option-tree/includes/ot-settings-api.php generates the form and it sets the form action to options.php (which is a wordpress core file). I was thinking about change the action to my custom php file and handle the save procedure and finally runs my own function. But this solution looks pretty ugly.
I wonder if there's another way to get the job done.
Thanks.
Thanks to #Sheikh Heera link (tutsplus) I could find a solution.
I think this is some kind of hack and I still don't know if it is the best way. Anyway I did this:
Create a file your-theme-settings.php in your theme lib folder.
Let Wordpress knows about your file by adding this code in your theme functions.php:
include_once('lib/your-theme-settings.php');
Add this code to your-theme-settings.php:
function your_theme_register_settings() {
register_setting('option_tree', 'option_tree', 'your_theme_validate_options');
}
function your_theme_validate_options($input) {
// do whatever you have to do with $input.
}
add_action('admin_init', 'your_theme_register_settings');
In step 3, I put 'option_tree' as 1st and 2nd argument of register_settings function, because I noticed that the Option Group and Option Name of OptionTree plugin is option_tree.
I'm not sure if this is the best solution, so I would be glad if you shares your ideas.

scripts not showing in custom wordpress theme

So, I've built my own theme for wordpress. Now, I'm trying to put my own plug-in on that theme which requires 3 javascript files. Upon reading the WP-Codex, it tells me the wp_register_script, wp_enqueue_script and add_action methods are the best way to go.
Now, in my plug-in file, I've done things such as:
function register_my_scripts() {
wp_register_script('script-1', plugins_url('path/to/script-1.js'), array('jquery'));
wp_register_script('script-2', plugins_url('path/to/script-2.js'));
wp_enqueue_script('script-1');
wp_enqueue_script('script-2');
}
add_action('wp_enqueue_scripts', 'register_my_scripts');
Nothing seems to show up on any of my template pages. I've even put this code on the main index page and still nothing. I've written something simple, straight from the codex like: wp_enqueue_script('jquery') on the page and still nothing shows up. Is there something I'm missing here? Why won't html for loading the scripts show up on my page?
Also, I'm running Wordpress 3.5.2
I enqueue my scripts like this on the functions.php file:
PHP
wp_enqueue_script ('customjs', '/wp-content/themes/blackboard/js/custom.js','','',true);
Please remember that the usage is:
<?php wp_enqueue_script($handle, $src, $deps, $ver, $in_footer); ?>
Try putting the wp_enqueue_script() on your functions.php just as a test. Please check that your path is correct too and check the source code to see if it's printing but just with a wrong path.
Note that the first line of code on this answer is the only thing I need to enqueue the script, nothing else as far as I know.
Let us know if this works for you, cheers.

Call to undefined function, get_settings wordpress

I have a Wordpress plugin with index.php that contains this line of code:
$plugindir = get_settings('home').'/wp-content/plugins/'.dirname(plugin_basename(__FILE__));
When I echo that code I get this output:
my.site.nl/wp-content/plugins/Tutorials
On the index page I also have a button that redirects to uploader.php,
inside uploader.php I have this code:
$plugindir = get_settings('home').'/wp-content/plugins/'.dirname(plugin_basename(__FILE__));
When I run the program I get an error that says:
Call to undefined function get_settings()
This error only applies on uploader.php, index.php works fine!
So it seems that I have to somehow let the uploader.php know that get_settings is a Wordpress function and it should recognize it, but I don't know how and I also don't know why the index page runs fine with the same code without including anything. I hope someone can help me out here.
index.php brings in all the WordPress functions as it loads. If you're loading another PHP file directly, you'll need to load in the WordPress functions by calling wp-load.php. See this answer (and the comments - you don't need to load all the files the answer mentions).
Try to use get_option('home') in place of get_settings('home') it may work.

Resources