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.
Related
I've the following code for adding advertisement before the_content of posts in my WordPress website.
<?php if (!empty($smof_data['ads_entry_top'])) { ?>
<div class="entry-img-300"><?php echo stripslashes($smof_data['ads_entry_top']); ?></div>
<?php } ?>
I want it to be added after the first paragraph, so wrote this function into functions.php:
add_filter( 'the_content', 'insert_after_first', 20 );
function insert_after_first( $content ) {
$content = preg_replace( "/<\/p>/", "</p>" . stripslashes($smof_data['ads_entry_top']) , $content, 1 );
return $content;
}
But it does nothing. What is wrong?
I changed my method and now add some content after first paragraph with below function and filter. I add it here maybe it helps someone:
add_filter('the_content', 'ata_the_content_filter', 10, 1);
function ata_the_content_filter($content)
{
$parags = explode('</p>', $content);
$parags[0] .= '<br>hello';// add whatever you want after first paragraph
$content_new = '';
foreach ($parags as $parag) {
$content_new .= $parag;
}
return $content_new;
}
I have a simple function which parse shortcode tags and its attribute,
but it has some problem in output.
Like, this is my content in a string with a shortcode inside it:
$content = 'This is lorem ispium test [gallery image="10"] and text continues...'
I want the result output like this:
This is lorem ispium test
----------------------------------------------
| This is output of gallery |
-----------------------------------------------
and text continues...
But now shortcode is not rendering where the shortcode is called, instead of this shortcode render at the top. like:
----------------------------------------------
| This is output of gallery |
-----------------------------------------------
This is lorem ispium test and text continues...
Kindly tell how do I render shortcode where it was called
function shortcode($content) {
$shortcodes = implode('|', array_map('preg_quote', get('shortcodes')));
$pattern = "/(.?)\[($shortcodes)(.*?)(\/)?\](?(4)|(?:(.+?)\[\/\s*\\2\s*\]))?(.?)/s";
echo preg_replace_callback($pattern, array($this,'handleShortcode'), $content);
}
function handleShortcode($matches) {
$prefix = $matches[1];
$suffix = $matches[6];
$shortcode = .$matches[2];
// allow for escaping shortcodes by enclosing them in double brackets ([[shortcode]])
if($prefix == '[' && $suffix == ']') {
return substr($matches[0], 1, -1);
}
$attributes = array(); // Parse attributes into into this array.
if(preg_match_all('/(\w+) *= *(?:([\'"])(.*?)\\2|([^ "\'>]+))/', $matches[3], $match, PREG_SET_ORDER)) {
foreach($match as $attribute) {
if(!empty($attribute[4])) {
$attributes[strtolower($attribute[1])] = $attribute[4];
} elseif(!empty($attribute[3])) {
$attributes[strtolower($attribute[1])] = $attribute[3];
}
}
}
//callback to gallery
return $prefix. call_user_func(array($this,$shortcode), $attributes, $matches[5], $shortcode) . $suffix;
}
function gallery($att, $cont){
//gallery output
}
Please note: it is not related to wordpress, it is a custom script.
I believe that the problem may be in your function gallery($att, $cont).
If that function uses echo or print instead of return, then it makes perfect sense to show up before the actual content does.
EDIT:
If you can't change the gallery code, then yes, you can use output buffering.
function handleShortcode($matches) {
...
ob_start();
call_user_func(array($this,$shortcode), $attributes, $matches[5], $shortcode);
$gallery_output = ob_get_contents();
ob_end_clean();
return $prefix . $gallery_output . $suffix;
}
Related readings:
PHP ob_start
PHP ob_get_contents
I am trying yo understand this function, as a preface to forking it to make similar functions for my own shortcodes. I understand how to define shortcodes and their functions. I also basically "get" what the original author is doing here: collecting parameters from the shortcode and assembling them into an HTML tag and returning that tag. It seems the order of the params is unimportant, but their names are.
However, when I am working with this code, it does not seem to understand which param is which. For example, the original docs say to use the shortcode like so:
[button link="http://google.com" color="black" size="small"]Button Text[/button]
But when I use this shortcode, I get:
<a href="Button Text" title="Array" class="button button-small button " target="_self">
<span>Array</span>
</a>
Here's my PHP:
if( ! function_exists( 'make_button' ) ) {
function make_button( $text, $url, $color = 'default', $target = '_self', $size = 'small', $classes = null, $title = null ) {
if( $target == 'lightbox' ) {
$lightbox = ' rel="lightbox"';
$target = null;
} else {
$lightbox = null;
$target = ' target="'.$target.'"';
}
if( ! $title )
$title = $text;
$output = '<a href="'.$url.'" title="'.$title.'" class="button button-'.$size.' '.$color.' '.$classes.'"'.$target.$lightbox.'>';
$output .= '<span>'.$text.'</span>';
$output .= '</a>';
return $output;
}
}
add_shortcode( 'button', 'make_button' );
See the documentation for Shortcode API, there clearly states that three parameters are passed to the shortcode callback function:
$atts - an associative array of attributes, or an empty string if no
attributes are given
$content - the enclosed content (if the shortcode is used in its enclosing form)
$tag - the shortcode tag, useful for shared callback functions
So the function definition should look like:
function make_button( $atts, $content, $tag ) {
// use print_r to examine attributes
print_r($atts);
}
The shortcode is explicitly looking for $text.
[button url="http://google.com" color="black" size="small" text="Button Text"]
Typically the variable that is set when you use the open/close shortcode is $content, per the Shortcode API. Another fix would be to change the shortcode to look for $content instead of $text.
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"]');
I have the following function in my theme's function page. basically what it does is look for any image in the post page and add some spans with css to dynamically create a pinterest button.
function insert_pinterest($content) {
global $post;
$posturl = urlencode(get_permalink()); //Get the post URL
$pinspan = '<span class="pinterest-button">';
$pinurlNew = '<a href="#" onclick="window.open("http://pinterest.com/pin/create/button/?url='.$posturl.'&media=';
$pindescription = '&description='.urlencode(get_the_title());
$options = '","Pinterest","scrollbars=no,menubar=no,width=600,height=380,resizable=yes,toolbar=no,location=no,status=no';
$pinfinish = '");return false;" class="pin-it"></a>';
$pinend = '</span>';
$pattern = '/<img(.*?)src="(.*?).(bmp|gif|jpeg|jpg|png)"(.*?) \/>/i';
$replacement = $pinspan.$pinurlNew.'$2.$3'.$pindescription.$options.$pinfinish.'<img$1src="$2.$3" $4 />'.$pinend;
$content = preg_replace( $pattern, $replacement, $content );
//Fix the link problem
$newpattern = '/<a(.*?)><span class="pinterest-button"><a(.*?)><\/a><img(.*?)\/><\/span><\/a>/i';
$replacement = '<span class="pinterest-button"><a$2></a><a$1><img$3\/></a></span>';
$content = preg_replace( $newpattern, $replacement, $content );
return $content;
}
add_filter( 'the_content', 'insert_pinterest' );
it does everything just fine. but is there a way to have it skip over an image with a certain class name in it like "noPin" ?
I would use preg_replace_callback to check if a matched image contains noPin.
function skipNoPin($matches){
if ( strpos($matches[0], "noPin") === false){
return $pinspan.$pinurlNew.'$matches[2].$matches[3]'.$pindescription.$options.$pinfinish.'<img$1src="$2.$3" $4 />'.$pinend;
} else {
return $matches[0]
$content = preg_replace_callback(
$pattern,
skipNoPin,
$content );
Another image attribute could conceivably contain noPin, if you are concerned about that edge case, just make the test in the if statement more specific.
You have to exclude the class noPin from the $pattern regexp :
$pattern = '/<img(.*?)src="(.*?).(bmp|gif|jpeg|jpg|png)"(.*?) \/>/i';
Has to become something like
$pattern = '/<img(.*?)src="(.*?).(bmp|gif|jpeg|jpg|png)"(.*?) (?!class="noPin") \/>/i';
Please check the regexp syntax, but the idea is to exclude class="noPin" from the searched pattern. Then your replacement will not be added to these images.