I want to display some html/php code via shortcode. After looking it up, I ended up with this :
add_shortcode('myshortcode', 'frontend_testshortcode');
function frontend_testshortcode() {
$email = get_field("field_email");
$name = get_field("field_name");
return $email;
}
I then insert [myshortcode] and the email is displayed on frontend, no problem.
However, the code I want to insert is much more complex, with dozens of variables and a lot of html (it is already written - until now i've been displaying it via shortcode but using a specific plugin. I want to get rid of the plugin).
If i do the following :
function frontend_testshortcode() {
$email = get_field("field_email");
$name = get_field("field_name");
<h1><?php echo $name; ?></h1>
<p><?php echo $email; ?></p>
}
It won't work. I understand that a shortcode requires "return $variable;" but i can't rewrite the whole code so as it can be inserted in a variable.
What would be the correct way to proceed?
Thank you!
Related
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;
}
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.
I want to display author name for "author meta tag" , I used following code to do this, But I always get an empty string for that:
$fname = get_the_author_meta('first_name');
$lname = get_the_author_meta('last_name');
$author = trim( "$fname $lname" );
if ( $author ) { ?>
<meta name="author" content="<?php echo $author; ?>">
<?php } ?>
How can I get the current displayed page/post author name ?
Thank you
You need to check first you if you get correct values from the get_the_author_meta(). So, you will know if the problem come from the trim/condition part, or from wordpress itself.
To do this, add in your code :
echo "Here is my result : ".get_the_author_meta('first_name')." ".get_the_author_meta('last_name');
Once this test done, i'm sure you'll need to edit your question.
Take this answer as a general advice for debugging, more than a solution for your problem.
Ok, got it. The name of the author is actually displayed by php in your HTML page, but it's encapsulated in a meta tag, which is only used in the head part of your page, and don't produce any visible output for the user.
Try to use a div tag instead of the meta one, and make sure you are writing inside the body part of your page.
$fname = get_the_author_meta('first_name');
$lname = get_the_author_meta('last_name');
$author = trim( "$fname $lname" );
if ( $author ) { ?>
<div>Author: <?php echo $author; ?></div>
<?php } ?>
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.
I haven't found anytihng in Google or the PHP manual, believe it or not. I would've thought there would be a string operation for something like this, maybe there is and I'm just uber blind today...
I have a php page, and when the button gets clicked, I would like to change a string of text on that page with something else.
So I was wondering if I could set the id="" attrib of the <p> to id="something" and then in my php code do something like this:
<?php
$something = "this will replace existing text in the something paragraph...";
?>
Can somebody please point me in the right direction? As the above did not work.
Thank you :)
UPDATE
I was able to get it working using the following sample:
Place this code above the <html> tag:
<?php
$existing = "default message here";
$something = "message displayed if form filled out.";
$ne = $_REQUEST["name"];
if ($ne == null) {
$output = $existing;
} else {
$output = $something;
}
?>
And place the following where ever your message is to be displayed:
<?php echo $output ?>
As far as I can get from your very fuzzy question, usually you don't need string manipulation if you have source data - you just substitute one data with another, this way:
<?php
$existing = "existing text";
$something = "this will replace existing text in the something paragraph...";
if (empty($_GET['button'])) {
$output = $existing;
} else {
$output = $something;
}
?>
<html>
<and stuff>
<p><?php echo $output ?></p>
</html>
but why not to ask a question bringing a real example of what you need? instead of foggy explanations in terms you aren't good with?
If you want to change the content of the paragraph without reloading the page you will need to use JavaScript. Give the paragraph an id.<p id='something'>Some text here</p> and then use innerHTML to replace it's contents. document.getElementById('something').innerHTML='Some new text'.
If you are reloading the page then you can use PHP. One way would be to put a marker in the HTML and then use str_replace() to insert the new text. eg <p><!-- marker --></p> in the HTML and $html_string = str_replace('<!-- marker -->', 'New Text', $html_string) assuming $html_string contains the HTML to output.
If you are looking for string manipulation and conversion you can simply use the str_replace function in php.
Please check this: str_replace()
If you're using a form (which I'm assuming you do) just check if the variable is set (check the $_POST array) and use a conditional statement. If the condition is false then display the default text, otherwise display something else.