In brief, I am trying to effectively insert a PHP function into the shortcode and having no luck.
The shortcode:
[res_map address=""]
The function:
<?php the_field('address'); ?>
What I have so far:
<?php echo do_shortcode('[res_map address="' . the_field("address") . '"]'); ?>
Any help on how to properly do this will be highly appreciated.
You may try something like this
$ret = the_field("address");
echo do_shortcode('[res_map address = "'. $ret .'"]');
In this case, your function must return a string, i.e.
function the_field($arg)
{
// ...
return 'something';
}
Because do_shortcode function expects string as it's argument and it's required. For example, it should be something like this
echo do_shortcode('[res_map address = "something"]');
Related
When using CF7 (Contact Form 7) is there any way to get the placeholder text from a php function?
I’ve tried using a shortcode to call the php function, but it doesn’t work.
Here is my test:
function.php code:
add_filter( 'wpcf7_form_elements', 'do_shortcode' );
add_shortcode( 'get_placeholder', 'get_placeholder_func' );
function get_placeholder_func () {
return "Hello world";
}
CF7 template:
[get_placeholder]
[text the-field placeholder [get_placeholder]]
First line works fine and outputs the text returned from the php function.
Second line doesn't work as it only outputs a end-bracket.
I know I can do it by using js/jQuery, but it is a bit messy.
Can anybody help? Thanks :)
I'm a little unclear as to why you would want to do this, but here's a method.
add_filter( 'wpcf7_form_elements', 'cf7_replace_a_string' );
function cf7_replace_a_string( $content ) {
// Name = Form Tag Name.
$str_pos = strpos( $content, 'name="the-field"' );
// If your form field is present.
if ( false !== $str_pos ) {
$placeholder = 'this is your placeholder';
$content = str_replace( 'placeholder="placeholder"', 'placeholder="' . $placeholder . '"', $content );
}
return $content;
}
Then your form tag would look like this:
[text the-field placeholder "placeholder"]
Why don't you try the following:
Define the function
function get_placeholder_func () {
return "Hello world";
}
Then create your own input for CF7
wpcf7_add_form_tag('custom_text_input', 'wpcf7_custom_text_input');
function wpcf7_custom_text_input($tag) {
if (!is_array($tag)) return '';
$name = $tag['name'];
if (empty($name)) return '';
$placeholder = get_placeholder_func();
$html = '<input type="text" name="'.$name.'" placeholder="'.$placeholder.'" />';
return $html;
}
Then, when editing CF7 you just have to call the input created
[custom_text_input name-of-input]
This shortcode will have name-of-input name and placeholder declared by the function get_placeholder_func()
Hope it works.
So this is a zany thing that I'm attempting to do, but what I am trying to achieve is querying out some info in WordPress with categories. If a post comes back with a parent category, then echo that out with the child category with a | seperator. If no parent category then just echo out the category.
Here's what I'm trying so far:
<?php
$categories = get_the_category();
$catName = $categories[0]->name;
$catParent = get_cat_name($categories[0]->category_parent);
if($catParent) {
$fullCat = $catParent '|' $catName;
} else {
$fullCat = $catName;
echo $fullCat;
?>
I know this is incorrect, but basically, I'm trying to wrap my head around how to accomplish this. I'm not sure how to combine two variables to use one and then add the separator within the two.
Was also wondering if maybe a ternary operator might be better here too? That was more of thought than anything and probably not that necessary to make this work correctly.
You're wanting to check if $catParent is empty so you can use the empty() function to do that check for you. Quick edit...you need periods (.) in between things you are concatenating.
Link to the docs.
if(empty($catParent)) {
$fullCat = $catParent . '|' . $catName;
} else {
$fullCat = $catName;
}
You must use '.' to concat.
Example:
$fullCat = $catParent. ' | '. $catName;
You can use . (Concatenation) operator to achieve this. Look at the code below.
<?php
$categories = get_the_category();
$catName = $categories[0]->name;
$catParent = get_cat_name($categories[0]->category_parent);
if($catParent) {
$fullCat = $catParent . '|' . $catName;
} else {
$fullCat = $catName;
echo $fullCat;
?>
I've written some code in php to scrape some preferable links out of the main page of wikipedia. When I execute my script, the links are coming through accordingly.
However, at this point I've defined two functions within my script in order to learn how to pass links from one function to another. Now, my goal is to print the links in the latter function but it only prints the first link and nothing else.
If I use only this function fetch_wiki_links(), I can get several links but when i try to print the same within get_links_in_ano_func() then it prints the first link only.
How can I get them all even when I use the second function?
This is what I've written so far:
include("simple_html_dom.php");
$prefix = "https://en.wikipedia.org";
function fetch_wiki_links($prefix)
{
$weblink = "https://en.wikipedia.org/wiki/Main_Page";
$htmldoc = file_get_html($weblink);
foreach ($htmldoc->find("a[href^='/wiki/']") as $a) {
$links = $a->href . '<br>';
$absolute_links = $prefix . $links;
return $absolute_links;
}
}
function get_links_in_ano_func($absolute_links)
{
echo $absolute_links;
}
$items = fetch_wiki_links($prefix);
get_links_in_ano_func($items);
Your function returned the value at the very first iteration. You will need something like this:
function fetch_wiki_links($prefix)
{
$weblink = "https://en.wikipedia.org/wiki/Main_Page";
$htmldoc = file_get_html($weblink);
$absolute_links = array();
foreach ($htmldoc->find("a[href^='/wiki/']") as $a) {
$links = $a->href . '<br>';
$absolute_links []= $prefix . $links;
}
return implode("\n", $absolute_links);
}
It's just an example but can I fix this issue ?
function echoText($text){
echo $text;
}
$text2 = echoText("Text");
echo "<h1>$text2</h1><br><h2>$text</h2><h3>$text</h3>";
But the result isn't <h1>, <h2> or <h3>, it's just simple text.
Your function is not returning the value, but echoing it out.
Try
function echoText($text){
return $text;
}
If I understand what you're trying to achieve correctly, you want this:
function echoText($text)
{
return '<h1>'. $text .'</h1>';
}
You can then use it:
$text2 = echoText('test');
echo $text2;
$text2 contains nothing (well, null technically), because echoText() returns nothing.
return a value from echoText() or otherwise assign a value to $text2.
I think you mean this:
<?php
function echoText($text){
echo $text;
}
$text2 = echoText("Text");
echo "<h1>".$text2."</h1><br><h2>".$text."</h2><h3>".$text."</h3>";
?>
You also need a return in your function.
I'm working on a wordpress theme and I'm trying to call the parent category's name to pull the appropriate page template.
I can get the call function to echo the correct name, but when I try to nest it the function doesn't run. I saw that I needed to use { } since I was already inside php but it still isn't working right. Can someone straighten me out?
This gives the correct output:
<?php $category = get_the_category();
$parent = get_cat_name($category[0]->category_parent);
if (!empty($parent)) {
echo '' . $parent;
} else {
echo '' . $category[0]->cat_name;
}
?>
. . . so I created a category_parent.php file with that in it.
This is what I'm trying to nest it in:
<?php get_template_part( ' ' ); ?>
Like this:
1.
<?php get_template_part( '<?php get_template_part( 'category_parent' ); ?>' ); ?>
or this
2.
<?php get_template_part( '{get_template_part( 'category_parent' ); }' ); ?>
Neither one works.
I really don't know if this is what you want as I did not try to make sense of what you are doing. However, generally speaking, you do this:
<?php get_template_part( get_template_part( 'category_parent' ) ); ?>
Edit:
I looked up what get_template_part() does in WP, and I think Felix Kling's answer is what you need. There is a big difference between sending something to the screen and assigning it to a variable.
<?php
echo 'filename';
?>
If you include that file, you will see filename in the browser. PHP knows nothing about it. (Okay, it could if you made use of output buffering functions, but that's besides the point...)
However if you do something like:
<?php
$x = 'filename';
?>
You can now use it in your function:
<?php
get_template_part($x);
?>
So what Felix is telling you to do is to put the logic that you currently have into a function. In this example:
<?php
function foo()
{
return 'filename';
}
get_template_part(foo());
?>
Now whatever value foo() returns will be sent to your get_template_part().
Taking your code:
$category = get_the_category();
$parent = get_cat_name($category[0]->category_parent);
if (!empty($parent)) {
$name = $parent;
} else {
$name = $category[0]->cat_name;
}
get_template_part($name);
You could take Felix's answer and put it into a file called category_parent.php, and then use it like:
require_once 'category_parent.php'
get_template_part(getName());
Honestly I am not so familiar with Wordpress, but it seems to me, you could do:
function getName() {
$category = get_the_category();
$parent = get_cat_name($category[0]->category_parent);
if (!empty($parent)) {
return '' . $parent;
} else {
return '' . $category[0]->cat_name;
}
}
get_template_part(getName());
konforce is correct about the syntax and, like konforce, I have no idea what you are trying to do. You do not need to use { } because your are not trying to dynamically name a variable and you certainly don't need to escape to php using <?php ?>, as (1) you are already in php and (2) it will stop interpreting PHP and assume html the second it hits the first '?>'.
There is no special syntax for nesting functions. Simply:
get_template_part(get_template_part('category_parent'));
is the syntax, but I have no idea what the function is or does, so I have no idea if that will work.
To debug, why don't you try this:
$parent = get_template_part('category_parent');
echo 'parent: ' . $parent . '<br />';
$result = get_template_part($parent);
echo 'result: ' . $result . '<br />';
When using variables in php strings, you will need to use double quotes ("). I assume option 2 should work then.