short code are not working on my custom theme - wordpress

i am not able to use any shortcode in my custom them is there any thing need to add in functions.php to enable shortcode feature??
i have created simple shortcode like in funxtions.php
function custom_shortcode(){
echo "hello there !!!";
}
add_shortcode('mycode', 'custom_shortcode');
but when i am trying to use my short code in post it shows simple [mycode] as display output
i dont know whats wrong in that i think i am missing to add something in functions.php for shortcode feature in my cutom theme

I thing you just need replace "echo" by "return", so:
function custom_shortcode(){
return "hello there !!!";
}
add_shortcode('mycode', 'custom_shortcode');
"Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call, since you cannot control when and where they are called from."
From: https://codex.wordpress.org/Function_Reference/add_shortcode

Related

Wordpress shortcode does not work

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.

is posible in wordpress post to replace a single word

I have on idea but i dont know if that is posible eg.
I'm writing a post in Wordpress site and I have to write a word too many times, is possible to write it once at the start and in the other part it to be written automatically by a shortcut or do I need a plugin. So if I change that word at the start all the other similar words to be changed automatically, like in word document action find and replace.
Yes, you can use a Shortcode for that. And you can write your own mini-plugin to do it.
The plugin would be simply:
<?php
/**
* Plugin Name: Single Word Shortcode
*/
add_shortcode( 'so', 'shortcode_so_22482571' );
function shortcode_so_22482571( $atts )
{
return "<a href='http://stackoverflow.com'>Stack Overflow</a>";
}
After activation, all [so] occurrences in your posts and pages will be converted to the Stack Overflow link.
You could write a shortcode, like brasoflio said, or you could just do exactly what you were thinking about in Word.... just do a find and replace in your document. Write the document in a text or markdown editor. Replace all instances of {abcdefg} with whatever text you want. Alternatively, you could use a find and replace plugin, or even write your own shortcode. But for something like this, I'd err on the side of laziness. Just write your text in a text editor, find and replace, then copy it in

wordpress add shortcode for a custom template

Would be possible to add a short code to a custom template to can be included in articles?
lets say I have a page.example.php custom template
What I want to create a plugin what would return this template when the shortcode is included in Article.
function get_Example_Template() {
$shortcode_content=get_template_part('page','example')
return $shortcode_content;
}
add_shortcode( 'shortcode_content', 'get_Example_Template');
Declare a variable in your plugin that is initialized to false. The first time your function is called, set it to true. Each time the function is called, you'll only proceed if that variable is false.
Answer is found here: Calling WordPress get_template_part from inside a shortcode function renders template first
The issue is that get_template_part immediately echoes, and shortcodes need a return. Output buffering should solve the issue.

I can't seem to override a function using template.php

I'm trying to alter the comment links in my Drupal output, and I think I have found the function I want to influence, which is function comment_node_view($node, $view_mode).
It is in the Comment module. The problem is I can't seem to effect it, when I try to override it by putting it in my Template.php file and add my theme_ to the function name? In my template.php it looks like this now:
function themename_comment_node_view($node, $view_mode)
if I take off the themename_ it causes an error saying I can't redeclare it. I can copy the comment module and edit it directly but I thought this was how I theme something?
Drupal themes can only implement theme functions (which include template preprocess and process functions) or alter hooks.
comment_node_view() is a hook, but it's not an alter hook (differently the hook name would end with "_alter").
Why cannot themes implement hook_node_view()?
Because hook_node_view() is invoked in comment_build_comment() using the following code:
// Allow modules to make their own additions to the comment.
module_invoke_all('comment_view', $comment, $view_mode, $langcode);
module_invoke_all('entity_view', $comment, 'comment', $view_mode, $langcode);
As it is also highlighted from the comment, module_invoke_all() invokes the hooks implemented in modules, not themes.
If you want to change how a comment is rendered, from a theme, you should create the comment.tpl.php template file for your theme.
The name spacing is 'hook_node_view' so you need to replace 'comment' (the name spacing used by the comment module) with your theme name:
function mytheme_node_view($node, $view_node)
Hooks:
http://api.drupal.org/api/drupal/includes--module.inc/group/hooks/7
hook_node_view:
http://api.drupal.org/api/drupal/modules--node--node.api.php/function/hook_node_view/7
Hope that helps :)

WordPress functions.php: how to apply update_option()?

I'm trying to set the default Image Link URL for my WP users so that it doesn't include the url link as a default. I've done some research, and I know the function is in the wp-admin/options.php:
update_option('image_default_link_type','file');
Rather than mess with the core files, I'd like to put this into the functions.php, but never know the proper way to implement stuff like this! This is what I have so far in my functions.php:
<?php
update_option('image_default_link_type','none');
?>
This obviously doesn't work: it needs the proper setup! What is the correct way to implement this in functions.php?
Also: I'd like to know the strategy for figuring out how to implement functions like this in the future by myself? For example, I never know whether or not I'm supposed to use add_filter or do_action, and how I need to pass the parameters. I've yet to find a book or post out there that explains this very well, and can show me by example. Any good leads on this would be awesome too!
Start with the Wordpress codex. Visit the plugin API (which is really what you are doing) that explains Hooks, Actions and Filters. Then see the Action Reference which provides your list of hooks.
Here you will find the hook update_option_OPTIONNAME. Description from codex:
Runs after a WordPress option has been update by the update_option
function. Action function arguments: old option value, new option
value. You must add an action for the specific options that you want
to respond to, such as update_option_foo to respond when option "foo"
has been updated.
Adding code from asker's comment:
function inventory_linkurl_setting() {
update_option('image_default_link_type','none');
}
add_action('admin_init', 'inventory_linkurl_setting'); ?>

Resources