Escape ACF fields with new lines - wordpress

I'm using a Text Area field within ACF.
I've set the "New Lines" option to "Automatically add paragraphs".
Here's how the PHP looks within my template file:
<?php
$notice = '<p>Some text.</p>';
$text = get_field( 'text', 'option' );
if ( $text ) {
echo str_replace( '<p>', '<p class="footer__paragraph">', $text );
}
else {
echo $notice;
}
?>
You'll see that I'm using str_replace to add a custom CSS class to the <p> tags.
I'd usually escape output, like so <?php echo esc_html( $text ); ?>. This obviously doesn't work because autop adds a <p> tag to each line.
Is it possible to escape output whilst maintaining my custom classes?

Yes it's feasible by whitelisting the tags and attributes you're interested in. Wordpress has a special function for that:
wp_ksesDocs
Here's how it works! You could pass it an array of tags that you want to accept while escaping every other tags and attributes. For example if you only need to keep you p tag and your custom class and escape everything else, then you could do something like this:
$text = get_field( 'text', 'option' );
$whitelist_tags = array(
'p' => array(
'class' => array('footer__paragraph')
)
);
echo wp_kses( $text, $whitelist_tags );
This will keep your p tag with your custom class and escape every other tags and attributes.
Let me know if that was what you were looking for!

Related

WordPress - Add tags taxonomy to comments

I'm working on a project that requires comments to have tags and be searchable by tags. Is there a way to implement it in WP, or should I look for some workaround (like create child post type instead of comments and apply tags to it)?
If there is, how can I do it?
Thank you.
You can use comment meta to store and retrieve tags of a particular comment.
First of all, add the tag field to the comment form and populate the tags. The following code will add a "select" field immediately after comment textarea and populate it with the tags.
add_filter( 'comment_form_defaults', 'change_comment_form_defaults');
function change_comment_form_defaults( $default ) {
$commenter = wp_get_current_commenter();
$out = '<label for="comment_tags">Tags:</label><select name="comment_tags" multiple>';
foreach ( get_tags() as $tag ) {
$out .= '<option value="<?php echo $tag->term_id; ?>"><?php echo $tag->name; ?></option>';
}
$out .= '</select>';
$default[ 'comment_field' ] .= $out;
return $default;
}
The comment_post action is triggered immediately after a comment is stored in the database. You can use it to store post meta.
add_action('comment_post', 'add_tags_to_comment', 10, 2);
function add_tags_to_comment( $comment_ID, $comment_approved ) {
foreach($_POST["comment_tags"] as $comment_tag) {
add_comment_meta( $comment_ID, "comment_tag", $comment_tag );
}
}
Instead of storing the selected tags as an array in a single record, I prefer to store each tag as a separate record. This will make it easier to search the comments based on tags.
When you want to retrieve the tags of a comment, You can get_comment_meta
$tags = get_comment_meta($comment_ID, "comment_tag");
foreach($tags as $tag_id) {
$tag_term = get_term($tag_id, 'post_tag');
echo $tag_term->name;
}
Use WP_Comment_Query to search comments based on tags.
$tags = array(1,32,5,4); /* Replace it with tags you want to search */
$args = array(
'meta_query' => array(
array(
'key' => 'comment_tag',
'value' => $tags,
'compare' => 'IN'
)
)
);
$comment_query = new WP_Comment_Query( $args );
Hope this helped you.

How to add placeholder in buddypress registration ? How to make field description as input placeholder?

How to add placeholder in buddypress registration?
Can we use field description as placeholder for input field?
In register.php under bp-template.These two lines of code make xprofile
$field_type = bp_xprofile_create_field_type( bp_get_the_profile_field_type() );
$field_type->edit_field_html();
How to edit them.Where these lines are connected.How to edit x-profile field.
Or even better yet, combining these two answers. Simply add this to your functions.php:
function wp_add_placeholder($elements){
$elements['placeholder'] = bp_get_the_profile_field_name();
return $elements;
}
add_action('bp_xprofile_field_edit_html_elements','wp_add_placeholder');
There is always a better way to do it then editing the core:
function wp_add_placeholder($elements){
$elements['placeholder'] = 'Placeholder';
return $elements;
}
add_action('bp_xprofile_field_edit_html_elements','wp_add_placeholder');
just struggled with this issue and fastest thing i came up were override classes that handles different fields:
First you need to find class named BP_XProfile_Field_Type_Textbox (for textbox). In bp v2.0.2 it can be found on bp-xprofile/bp-xprofile-class.php:2358. Copy whole class in your functions and rename class name as you desire. let's say i renamed it as CUSTOM_BP_XProfile_Field_Type_Textbox. Inside this class there are function called public function edit_field_html( array $raw_properties = array() ).
Replace:
$html = $this->get_edit_field_html_elements( array_merge(
array(
'type' => 'text',
'value' => bp_get_the_profile_field_edit_value(),
),
$raw_properties
) );
?>
<label for="<?php bp_the_profile_field_input_name(); ?>"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php esc_html_e( '(required)', 'buddypress' ); ?><?php endif; ?></label>
<?php do_action( bp_get_the_profile_field_errors_action() ); ?>
<input <?php echo $html; ?>>
<?php
}
With that:
$required = bp_get_the_profile_field_is_required() ? ' ' . esc_html__( '(required)', 'buddypress' ) : '';
$html = $this->get_edit_field_html_elements( array_merge(
array(
'type' => 'text',
'value' => bp_get_the_profile_field_edit_value(),
'placeholder' => bp_get_the_profile_field_name() . $required
),
$raw_properties
) );
?>
<?php do_action( bp_get_the_profile_field_errors_action() ); ?>
<input <?php echo $html; ?>>
<?php
}
Next thing you need to do is override function that hendles field classes. Copy function called bp_xprofile_get_field_types into your theme and rename it. let's say i renamed it as custom_bp_xprofile_get_field_types.
In fields array rename 'textbox' value from BP_XProfile_Field_Type_Textbox to CUSTOM_BP_XProfile_Field_Type_Textbox (the class you created and modified).
Finl thing you need to do is override function that print final result. Copy function called bp_xprofile_create_field_type into your theme and rename it. let's say i renamed it as custom_bp_xprofile_create_field_type.
In this function replace:
$field = bp_xprofile_get_field_types();
with:
$field = custom_bp_xprofile_get_field_types();
to use your modified field output
In register.php use just created new function instead of original buddypresses one so the final result will be:
$field_type = custom_bp_xprofile_create_field_type( bp_get_the_profile_field_type() );
$field_type->edit_field_html();
It is good to copy register.php to YOUR_THEME/buddypress/members/register.php to not lose your changes after updating the bp plugin.
If you like to modify other fields just rename field's class in classes array in custom_bp_xprofile_get_field_types and copy bp's class of that field and rename it as in fields array.
Hope that helps. There might be better way, but i didn't find any.

Wordpress: Function to add images in text editor

I'm really new to Wordpress and slowly getting my head around it but I've come across a problem and I don't know how to fix it.
I'm creating pages using the text editor in the backend and I'm pasting html in there. All's working well and I can output images using this function in functions.php (i found this snippet somwhere but can't remember where)
// Create the shortcode
function template_url( $atts, $content = null ) {
return '<img src="'. get_template_directory_uri() .'/'. $content .'" alt="" />';
}
// Add as shortcode
add_shortcode("template", "template_url");
and i call it like this
[template]images/my-image.jpg[/template]
The problem is how do i add some alt text and also add a class to the image
You can add shortcode with your custom attributes like this:-
Add Following code for adding shortcode:
function template_url( $atts, $content = null )
{
extract(
shortcode_atts( array(
'alt' => '',
'width' => '',
'height' => ''
), $atts )
);
//And this variable you can use like this:-
return '<img src="'. get_template_directory_uri() .'/'. $content .'" alt="'.$alt.'" width="'.$width.'" height="'.$height.'"/>';
}
add_shortcode("template", "template_url");
And use this shortcode in editor:-
[template alt="this is alter text" width="400" height="300"]images/my-image.jpg[/template]
I hope this will be helpful.

How to remove wrapping p tags from acf wysiwyg field string for later custom excerpt processing

I have a WYSIWG field with the advanced custom fields plugin. When i query it with :
<p class="bran-hn news-excerpt"><?php echo custom_field_excerpt('news_post'); ?></p>
the output looks like that:
<p class="bran-hn news-excerpt"></p>
<p>Worf, It's better than music. It's jazz. Mr. Crusher, ready a collision course with the Borg ship. This is not about revenge. This is about justice. The Federation's gone; the Borg is everywhere! In all trust, [...]</p>
<p></p>
But i would have expected and want something like:
<p class="bran-hn news-excerpt">Worf, It's better than music. It's jazz. Mr. Crusher, ready a collision course with the Borg ship. This is not about revenge. This is about justice. The Federation's gone; the Borg is everywhere! In all trust, [...]</p>
i tried to add
$text = strip_tags ($text);
right before the strip_shortcodes call into the function querying the wysiwyg custom field:
function custom_field_excerpt($title) {
global $post;
$text = get_field($title);
if ( '' != $text ) {
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$excerpt_length = 35; // 20 words
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
return apply_filters('the_excerpt', $text);
}
but also no effect. so is there a way to strip the wrapping p tags while keeping possible tags inside the strings text body like e.g. a tags for links. Best regards Ralf
I know this is old but maybe it helps someone else.
I had the same issue and using "false, false" as second and third parameter for get_field did the trick.
$myField = get_field('field_name', false, false);
echo '<p class="custom-class">'.$myField.'</p>';
Maybe a simple string replace?
$text = get_field('title');
$stripped_text = str_replace(array('<p>','</p>'),'',$text);
Another option is PHP's strip_tags function. This will remove all tags.
https://www.php.net/manual/en/function.strip-tags.php
$stripped_text = strip_tags( get_field( 'field_name' ) );
remove_filter ('acf_the_content', 'wpautop');

Custom Fields/Values displaying out of order

I'm using the Types Wordpress plugin to enable custom fields. The plugin allows you to rearrange the order of the images in the admin page editor. Here's my code in my single.php to display multiple images in the custom field and have a link to itself to also use Fancybox:
<?php
$mykey_values_img = get_post_custom_values('wpcf-image');
if ($mykey_values_img != null) {
foreach ( $mykey_values_img as $key => $value ) {
?>
<img src="<?php echo $value; ?>" />
<?php
} //foreach
} //if
?>
Problem:
Right now this code works perfectly on my local copy running on MAMP. However, when I put it online hosted on iPage, the images are out of order. I don't know what's causing this discrepancy. When I use the shortcode from Types to display the images instead of the php above they are in order, but I don't have the option of using Fancybox. I was also wondering if there is a better way to display an image from Wordpress that will insert the alt tag for the image.
I just encountered this problem too, and your first answer led me to a tighter solution.
I also used types_render_field(), but if you include a 'raw' parameter, you can avoid the string manipulation.
$images_raw = types_render_field('image', array('raw'=>'true','separator'=>';'));
$images = explode(';', $images_raw);
foreach ($images as $link) {
echo '' . $link . '">"';
}
Then, if you're nasty, you can get the ID of the attachment from its SRC. Using that ID, you can get all the info you need about that attachment, like a caption, or whatnot.
I figured out a work around to get it working. Its not ideal but it works.
The Types plugin came with its own php function to display the custom field called types_render_field. It displayed my images in order. To get fancybox working I had to do sort of a hack on the string. Here's the code:
$images = ( types_render_field( "image", array( 'size' => 'full', 'alt' => get_the_title(), 'title' => get_the_title() ) ) );
$imgArray = explode(">", $images);
foreach ( $imgArray as $value ) {
$pos1 = strpos($value, 'src="', 0)+5;
$pos2 = strpos($value, '" ', $pos1);
$link = substr($value, $pos1, $pos2 - $pos1);
echo ''.$value.">";
}

Resources