I am learning to write a WordPress short code.
In my function I created a big table and some CSS. When I put my short code into posts, the position of the table is always on the top of the posts, regardless of where I put the code in the editor.
How can I fix this?
Shortcode content should be return. not echo. user below:
function shortcodetest(){
return "<table><tr><td>test</td></tr></table>";
}
add_shortcode("shortcode","shortcodetest");
Do not use echo :
function shortcodetest(){
echo "<table><tr><td>test</td></tr></table>";
}
add_shortcode("shortcode","shortcodetest");
Related
Background
I use php CPT templates with ACF. Usually i get ACF variables and depending on these variables i use get_template_part() to display content. in other cases i just use echo to deliver content. So far, this is working out for years now.
Problem
A customer is using DigiMember. DigiMember is a membership plugIn that uses shortcodes to protect/hide part of the content. This works only if the standard content output is used. so my get_template_part() sections interpret shortcodes as expected but my simple echo output sections do not and are not protected. i assume that digimember hooks itself in the main content function/loop in order to filter allowed content.
Shortcodes
So far i successfully testet the do_shortcode() php function with the digimember shortcodes. anything within is protected. My idea was to collect all output in a variable with ob_start and then output this variable within the do_shortcode() function, but this seems a bit off?!?
My question
Is there some kind of wrapper function in wordpress to output html/text instead of using simple echo? so that any string or ob_start buffer will be processed with all usual wordpress filters. ... or is my brain on a wrong path?
Thanks for your suggestions :)
You could try doing something like:
$return = '<div>';
$return .= do_shortcode('[yourshortcode]');
$return .= '</div>';
echo $return;
or
return $return // if your using the add_shortcode('short code function
so we are making a wordpress site that automatically adds a link to a comment every time a comment is created. The theme we are using is html5 blank. I have written several lines of code into its function.php. however it is not working right now, here is what i wrote:
function bo_wrap_comment_text($content) {
$content = get_comment_text();
$texta = urlencode(get_comment_text());
$textb = "http://www.google.com/search?btnI&q=" + $texta;
return ''.$content.'';
}
add_filter('wp_insert_comment','bo_wrap_comment_text',1000);
a separate code(this one works) is tested here: http://phpfiddle.org and the code is:
<?php
$texta='i hate apple';
$textb=urlencode($texta);
$textc= "http://www.google.com/search?btnI&q=".$textb;
echo ''.$texta.'';
?>
the wordpress site is here. It is simply a static front page. the title you see there is the title of comment area. everything related with blog posts has been hidden using css. we have manually added links to several comments as prototyping. what we want is replace manual work with wordpress functions. I would really appreciate any help.
You need this hook instead to edit comment data:
http://codex.wordpress.org/Plugin_API/Filter_Reference/preprocess_comment
I've been stuck on this for a while. I'm working on a wordpress site where I wrote the theme from scratch, I use php calls to get the wordpress functionality that I need in certain sections.
I'm trying to use a plugin, but calling it via
echo do_shortcode('[STORE-LOCATOR]');
just isnt working. Even when I switch to the default template and post that code, it still doesnt work. It simply echoes "[STORE-LOCATOR]"
Any help would be greatly appreciated.
[STORE-LOCATOR] is probably not a 'shortcode' in WordPress sense.
I encountered this on different plugin, Stream media player. They use the same syntax as shortcodes, but they are actually not.
Try using:
echo apply_filters( 'the_content',' [STORE-LOCATOR] ');
instead of do_shortcode, and see if it helps.
do_shortcode() returns a string.
I get it working by doing:
<?php echo do_shortcode(...); ?>
This is specific to the Store Locator plugin, not do_shortcode in general.
apply_filters can be an acceptable workaround for other plugins, but this does not work for Store Locator; you will only see an empty space and some controls. This is because it is looking for that shortcode in the page/post body to determine whether or not to include all of its js references at the top of the page. And without these references, nothing will work. See the sl_head_scripts function in sl-functions.php.
To change this behavior, simply modify that function to match based upon page title instead. In my instance I wanted it only on a "shop" page, so I commented out the entire $on_sl_page test and replaced it with this:
$on_sl_page = ( strpos($pagename, 'shop') === 0 );
I then called it from my page with apply_filters as indicated in the other answer:
echo apply_filters( 'the_content','[STORE-LOCATOR]');
And this appears to work perfectly.
echo do_shortcode('[STORE-LOCATOR][/STORE-LOCATOR]');
Try using shortcode after the WordPress environment has been set up.
function my_function() {
echo do_shortcode('[STORE-LOCATOR]');
}
add_action('wp', 'my_function');
If you're writing the whole thing from scratch, you'll want to make sure that the function you create is in the root php file of your plugin. The function might look something like this, but you'll have to sub in whatever logic you're using to arrive at the store location:
<?php
function doCoolStuff () {
$var1 = "value1";
$var2 = "value2";
$output = $var1+$var2;
}
return $output;
}
add_shortcode('SOTRE-LOCATIOR', 'doCoolStuff');
?>
Then in your template put the code:
<?php echo do_shortcode('[STORE-LOCATOR]');?>
Happy coding and good luck!
I want to display certain text in all my posts except if it's in this one category. How do I do that? Oh yea I almost forgot I want to include the title of the post in the text. So I think I need to use echo, cat='-5', and or something?? I don't know how to form it though. Thanks!
You could use the Wordpress function in_category(). When you use it inside the loop, it returns true if the current post is a member of the category you passed it.
<?php
if ( in_category('my-category'))
{
// don't output text
} else {
// do output text
}
?>
Do you require the text to be completely locked away, or just hidden from view? If you only need it hidden from view (but accessible to anyone who chooses to pry) then you may be able to do it very quickly using css.
If you have coded your theme - or are using someone else's - that adds helpful styles into the header, you may have a lot to work with already. For example, this is a body declaration generated by the Thematic theme:
<body class="wordpress y2011 m01 d31 h12 archive category category-orthopaedics">
Say you have a chunk of content to hide:
<div class="text_to_hide">This is what gets hidden.</div>
Then you declare the CSS as:
.category-orthopaedics .text_to_hide { display: none; }
Anyone familiar with WordPress shortcodes? I could really use a hand! I've inserted the following code into my functions.php file for the theme I'm using...
function create_slideshow_header($atts, $content = null){
return '<div class="item_heading">'.$content.'</div>';
}
add_shortcode('slideshow_heading', 'create_slideshow_header');
function create_slideshow_white_header($atts, $content = null){
return '<span id="dyn">'.$content.'</span>';
}
add_shortcode('slideshow_heading_white', 'create_slideshow_white_header');
function create_slideshow_content($atts, $content = null){
return '<div class="item_content">'.$content.'</div>';
}
add_shortcode('slideshow_content', 'create_slideshow_content');
Now, I was led to believe by several tutorials that this should allow me to insert the following into the text editor in the WP backend...
[slideshow_heading]SLIDESHOW HEADER[/slideshow_heading]
...and the SLIDESHOW HEADER text would be wrapped in the appropriate HTML.... but it's just displaying the above as regular text. I've cleared my cache, etc...
Is there something I'm doing wrong? Thanks in advance!
SOLUTION
I failed to mention that I was using the page.ly MultiEdit plugin--which uses "custom fields" to create extra editable regions. WordPress conveniently doesn't parse shortcodes in custom fields. Normally, you can create a filter for each custom field, but since this is a plugin, you can just edit the multiedit.php file, and change line 63 from
echo $GLOBALS['multiEditDisplay'][$index][0];
to
echo apply_filters('the_content',$GLOBALS['multiEditDisplay'][$index][0]);
With a little work, you can turn Wordpress into a truly amazing CMS!
I actually checked out your code, it works for me. Your usage is right.
Try adding a die() in there to see whether the method is called in your case.