Simple Shortcode (Wordpress) does not work - php

This is my "code":
function whoop_function(){
return "whoop whoop!";
}
add_shortcode('whoop', 'whoop_function' );
Now when I want to use it in a post, all I get is:
[whoop]
As you can see I am very new and unused to it so maybe the answer is really simple, maybe I've just miss a thing in advance.
I both checked defining the function in functions.php and also in content.php

For shortcode function echo is required.
Please check with below code
function whoop_function(){
$responseData = "whoop whoop!";
echo $responseData;
return true;
}
add_shortcode('whoop', 'whoop_function' );

put your code in themes/function.php files and remove from content.php as function is duplicated so function already defined PHP error occurred.
function whoop_function(){
return "whoop whoop!";
}
add_shortcode('whoop', 'whoop_function' );
and add your shortcode [whoop] in any page content section.
if u use do_shortcode('[whoop]'); then echo it like below.
<?php echo do_shortcode('[whoop]'); ?>

Your approach is correct. I think you are using it in a template. Just need to use it as mentioned below :
In files:
<?php echo do_shortcode('[whoop]'); ?>
In admin pages or posts:
[whoop]
Fetch the content like this:
$post_content = get_post(get_id_by_slug('short')); // assuming that you have already defined get_id_by_slug function as you are using it in your code and "short" is a slug for post you are fetching content
$content = $post_content->post_content;
echo do_shortcode( $content );
You are fetching content without considering the short code. Update your code as above.
Hope it will work for you.

Related

How to use shortcode in php code to get value via shortcode for php code?

I'm trying to get postid via shortcode to get parent category of that post.
Here is my code
I tried this code for now but not get any luck, I don't know what is going wrong. Please consider my request I'm very new to PHP and WordPress.
<?php
$category_detail=get_the_category('[field parent-id]');//$post->ID
foreach($category_detail as $cd){
echo $cd->cat_name;
}
?>
I want to print my post id like this
$category_detail=get_the_category('4');//$post->ID
I can't use PHP code to get post id as my other format is make with shortcodes so please help me in this. Thanks (Sorry for poor English)
Update : I tried this code also but no luck
<?php
$id = do_shortcode('[field parent-id]');
$category_detail=get_the_category($id);//$post->ID
foreach($category_detail as $cd){
echo $cd->cat_name;
}
?>
The problem is this shortcode is trying to get the parent-id, but the parent ID of what? You need to tell It, so try this (untested)
global $post;
$parent = $post->post_parent;
$id = do_shortcode('[field '.$parent.']');
$category_detail=get_the_category($id);//$post->ID
foreach($category_detail as $cd){
echo $cd->cat_name;
}

Put the output of one PHP function into another in Wordpress

I am using a plugin that outputs data with a simple tag reference
<?php echo $property['building_brochure']; ?>
This outputs the reference number for an uploaded pdf which in this example is 352.
I want to return the actual link for this, the following works
<?php echo wp_get_attachment_url( 352 ); ?>
However, I want the combine these so on the single page template for the entry the relevant PDF is called i.e.
<?php echo wp_get_attachment_url( 'building_brochure' ); ?>
Is there a straightforward way of doing this?
You need to place the function as input into the other function the way Spectre-d has shared in comments.
i.e
<?php echo wp_get_attachment_url( $property['building_brochure'] ); ?>
However if you face issues,like not correct attachment_URL, then it could be Type issue i.e. first function might be outputting string and not integer as expected by attachment_url function in that case I recommend:
<?php
$pdf_id = (int)$property['building_brochure'];
echo wp_get_attachment_url( $pdf_id );
?>

Using shortcode breaks RSS feed

I had successfully created shortcode on my wordpress website to use it in the posts. But I later realised that it is breaking my rss-feed.
Here is the shortcodes.php
<?php
function disclaimer() {
$disc = " <p style='font-style:italic;'> <strong>Disclaimer</strong> :</p>";
return $disc;
}
add_shortcode('disclaimer', 'disclaimer');
?>
Here is how I am including it on the functions.php
<?php
include 'shortcodes.php';
So now, I am getting this error when I access my rss URL on my browser
So when I removed this include 'shortcodes.php'; my rss feed started working. But then I also want my shortcode to work. Am I doing anything wrong here? Any help would be highly appreciated.
You can change your function so that the disclaimer is not added on feed page
function disclaimer() {
if ( ! is_feed() ) {
$disc = " <p style='font-style:italic;'> <strong>Disclaimer</strong> :</p>";
return $disc;
}
}
Edit:
Then it is not a problem with the code, but some problem with the file.
Make sure there is not extra space after the closing ?> php tag. Or better, try to remove it completely.

site_url with path and custom variable

I'm not very familiar with PHP and have been trying my hardest to figure out how to create this URL. So far, this is working:
<?php echo site_url($p->post_title) ?>
Where post title is defined by the Mapify.it Wordpress plugin. The result is:
http://siteurl.com/post_title
What I'd like to do is add a string before it, ideally ?s= or /search/, but when I try to add this before $p->post_title I'm still generating the above URL. Variations such as:
<?php echo site_url('?s=', $p->post_title) ?>
<?php echo site_url('/search/', $p->post_title) ?>
produce http://siteurl.com/?s= and ignore the variable. Nothing seems to do what I want.
What am I doing wrong?
Hope you need the following url format,
http://siteurl.com/?s=Here come the post title
So,
<?php echo site_url("?s=".$p->post_title) ?>
OR
<?php echo site_url("/search/".$p->post_title) ?>
should work.
Found it!
<?php echo site_url('?s='), $p->post_title ?>
Instead of adding custom URL Parameters directly, I'd suggest you to use WordPress built-in function add_query_arg(), it's more cleaner.
Here is an usage example:
$url = get_site_url();
$params = array(
's' => $p->post_title
);
echo add_query_arg($params, $url);
You can specify multiple parameters this way.
For ref: Check add_query_arg()

How to handle HTML code that repeats a lot

I have some HTML code portions that repeat a lot through pages. I put this html code inside a function so that it is easy to maintain. It works perfectly. I, however feel this may not be very good practice.
function drawTable($item){
?>
HTML CODE HERE
<?php
}
I also run into the problem that when I want to return data using json the following won't work as it will be NULL:
$response['table'] = drawTable($item);
return json_encode($response);
What's the correct way to handle HTML code that repeats a lot??
Thanks
You may want to look into using templates instead of using ugly heredoc's or HTML-embedded-within-PHP-functions, which are just plain unmaintainable and are not IDE-friendly.
What is the best way to include a php file as a template?
If you have a repeating segment, simply load the template multiple times using a loop.
Although templates help with D.R.Y., the primary focus is to separate presentation from logic. Embedding HTML in PHP functions doesn't do that. Not to mention you don't have to escape any sort of quotes or break the indentation/formatting.
Example syntax when using templates:
$data = array('title' => 'My Page', 'text' => 'My Paragraph');
$Template = new Template();
$Template->render('/path/to/file.php', $data);
Your template page could be something like this:
<h1><?php echo $title; ?></h1>
<p><?php echo $text; ?></p>
function drawTable( $item ) { return '<p>something</p>'; }
function yourOtherFunction() {
$response['table'] = drawTable($item);
return json_encode($response);
}
Use this function definition
function drawTable($item){
return 'HTML CODE HERE';
}
Called with
print drawTable($item);
Which will also work for your json return value.

Categories