Wordpress- Echo PHP into Shortcode - php

I've run into a little bit of a snag with shortcodes. I want it to take the variable and put it in place for the text that should usually go there, but instead of working it just doesn't load it up, even though I've tested it with the echo to see if it's putting anything out and it is.
<?php
$artistslug = the_field('artist_cat_slug');
echo $artistslug; // Here for test reasons
echo do_shortcode('[product_category category="'.$artistslug.'"]');
?>
Any help would be greatly appreciated.

Most probably, the_field() displays the value. The plugin you are using might have a corresponding function to return the value instead, for e.g. get_the_field(). Use that instead.
Edit after clarification in comments
From the documentation for the_field() (emphasis mine):
Displays the value of the specified field. (this is the same as echo get_field($field_name))
Exactly. Use get_field() instead.

Related

When do i need to use echo with strings in WordPress?

I've noticed that my php inside of a WordPress site work without actually echoing some strings in specific situations.
Example:
link
link
Both code output the permalink on my wordpress website. (Versions: PHP 7.17, WP 4.9.8)
Question:
When do I need to use echo and which security concerns do I need to be aware of?
Why both output the URL is because you are using the_permalink() - which echos the permalink. That in turn means it is NOT getting run through your esc_url -
Instead, you need to use echo esc_url( get_the_permalink() ); - where get_the_permalink() does not echo, but returns - therefore it will get passed into esc_url, which will then require the echo
The only difference between the_permalink and get_the_permalink: one echo's, one returns.
Note that WordPress is full of handy functions that work this same way:
the_ID() vs get_the_ID(),
the_title() vs get_the_title(),
etc...
Special case:
the_content() vs get_the_content()
Be aware however that the_content, while naming follows the same pattern and does echo vs. return, the_content has an additional difference that it passes the content through the the_content filters (which does a lot of formatting, expands shortcodes, etc).
According to the official source the function the_permalink() Displays the permalink for the current post.
So it has built-in functionality to print output without echo so you can use whatever text you like as the link text, in this case, “permalink”.
permalink
Echo
The echo() function outputs one or more strings.
echo is not actually a function (it is a language construct), so you
are not required to use parentheses with it. echo (unlike some other
language constructs) does not behave like a function, so it cannot
always be used in the context of a function. Additionally, if you want
to pass more than one parameter to echo, the parameters must not be
enclosed within parentheses.

PHP: is echo from an array inside another array ok?

Tha following is working in index.php, but is it correct?
Before the html tag:
$la= array();
$la['index.php'] = 'Start page';
(Actually this is another language library that is included)
Then inside the header:
<title><?php echo $la[$_SERVER['PHP_SELF']];?></title>
For me the part "$la[$_SERVER['PHP_SELF']]" seams strange, but its working. The title is there in my browser. Is it good practice?
Yes, current code works. If it's good practice is up for debate.
PHP (like many other language) will evaluate the statements in order.
Everytime you use the brackets you are really using the arrays index operator where the index acts as the parameter.
Your code will first evaluate the $_SERVER['PHP_SELF'] statement which probably returns 'index.php'. The next call will be $la['index.php'] (since that was what your inner statement returned. This will in turn return the value 'Start page' which is what is sent to the echo.
There's nothing wrong with your code. The superglobal $_SERVER['PHP_SELF'] holds the name of the current file. It's not very secure because it can be manipulated to execute arbitrary code if you inject it without sanitizing it properly.

How to concatenate 2 function in php

I have the following code:
<?php
if(isset($_GET['function'])){
$_GET['function']();
}
?>
So if i entered this url:
http://localhost/?function=phpinfo
I will see the phpinfo function output on the screen.
can i have a way to concatenate 2 function in the url like this example:
http://localhost/?function=shell_exec('ls') AND phpinfo
So i want to see the first function output..
If you may asking why i need this, is because i am pen testing an web application with this situation..
Thanks for the help..
With your given code example it is not possible to do what you want. All your functions, so shellexec('ls') and phpinfo will be interpreted as one string, which is then called as a function in by calling it with added parenthis.
The only way that I can think of is using a variable parameter list, rather than just a single parameter. Get all the GET parameters in the function, and loop through them, executing each one.

convert $var of function to string?

My goal just debug
function dbg($var){
echo "you have passed $var";
}
call dbg($test)
output:
you have passed test
call dbg("var")
output:
you have passed "var"
In php .anyone could help me to do that?
Try this if $var is a global variable:
function dbg($var){
echo "you have passed {$GLOBALS[$var]}";
}
Well, the second case is fairly straightforward - you're passing a string and you want to display the string. No worries.
But for the first case, I'm afraid the answer is: No you can't.
Once inside the function, PHP doesn't know anything about the variable that was passed into it other than the value.
I can't really see that it would be of much value though. It would be trivial to change your code to pass in a name and a value -- ie something like this:
function dbg($name,$value) {
print "You passed $name, and the value was $value";
}
dbg('test',$test);
That's not really all that great either though -- you may as well just use print_r() and friends.
If you really want more powerful debugging tools, you should look into xDebug. It's a proper debugging tool for PHP, which allows you to step through the code line-by-line, and see the contents of variables at any point during the program run (among many other good features). It also integrates nicely with several popular IDEs.

What is the best way to capture data returned from a function in PHP?

I am new to programming and learning with Wordpress.
the_title(); //outputs the title of the page
I want to capture the title of the page into a string variable so I can manipulate it with strtolower and str_replace functions.
The only way I have gotten it to work is with output buffering.
ob_start();
the_title();
$result = ob_get_clean();
echo str_replace(" ","-",strtolower($result));
/*there has got to be an easier way....
i dont fully understand why */
str_replace(" ","-",strtolower(the_title()));
What am I doing wrong?
If what you really are looking for is the wp_title function, the 2nd argument it takes is a boolean on whether or not it should display it or return it. Pass it false so it will return it to the var, then you can do this:
$mytitle = wp_title(null, false);
Otherwise, your only option is to find the function you're looking for and modify the source code.
There is no easier way. Your function does not return the string, it prints it, therefore you will have to use output buffering if you want to capture the output.
It's the difference between f1() and f2() in the following example.
// Returns a string, prints nothing.
function f1() {
return "String";
}
// Prints a string, returns nothing.
function f2() {
echo "String";
}
Wordpress is a HORRIBLE app to learn how to program from. It uses these global functions that "just work" but they do very specific tasks "inside 'The Loop'". As I say, this is a horrible example of what good code should be.
Thankfully (for you) there are other functions that just return the part you're looking for. Rather than me just writing what you need, you can read a full listing here. Take care that you note down which must be within the mythical Loop and which you can use anywhere.
As it happens there are even more ways to get the title, but I was really imagining for this example you would do something like:
$this_post = get_post($post); // I *think* $post is the post ID inside the loop though I could be wrong
echo $this_post->post_title;
But as another poster (correctly) says you can use a fairly simple wp_title() function to grab the current loop title.
This brings me back to perhaps wanting to explain why learning programming from Wordpress is a bad idea. They have so many damned way of doing the same damned thing that it's almost impossible to keep on top of things.
A blog is a really simple set of data (even moreso in WP's case because it isn't fully normalised) but rather than just having one way to output a title <?php echo $post->title; ?> you have umpteen ways, all doing subtly different things.
If you really want to learn how to program (instead of hacking your way around the crap that is the WP internals), creating a simple blog engine is fairly quick and fun... It's certainly how a lot of people get into a new language or framework.
And if you really want to have fun, have a look at Django.
Enough of the Wordpress rant. If you're fighting something like this in the future that doesn't have 100 ways of doing it, I really wouldn't recommend output-buffer-capturing. It uses up a whole buttload of resources for something relatively simple.
The easiest way can be as simple as just taking the source for the original function, sticking it in a new function and replacing the echo with return.
Just note there may be some database connectivity to handle that returning prematurely may break... So if the echo isn't the last statement, instead of returning right there, store the string as a variable and return at the end of the function.
just figured Id share my final solution with you guys.
This was to give my body tags unique id's in wordpress.*/
$title =wp_title(null,false);
echo strtolower(str_replace(' ','-',ltrim($title)));
//without the ltrim() 2 dashes are created before the title.
Almost every 'the_*' function in Wordpress has a 'get_the_*' counterpart. So, you just have to use
echo str_replace(" ","-",get_the_title());
And it's going to work like a charm. there's also get_the_excerpt(), get_the_content() and the_permalink() which somehow breaks the naming convention (God knows how many times I've written "get_the_permalink()" and got frustrated on why it didn't work)
Cheers!

Categories