Custom Field not working in value get_the_content? - wordpress

`
$metadescription = wp_trim_words( get_the_content(), 55, '');
add_post_meta($post_id, 'meta_description', '$metadescription', true);`
but output is $metadescription
anyone help me please
`$metadescription = wp_trim_words( get_the_content(), 55, '');
add_post_meta($post_id, 'meta_description', '$metadescription', true);``

Try out the below code:
For the variable, you don't need to use '' for that.
$metadescription = wp_trim_words( get_the_content(), 55, '');
add_post_meta($post_id, 'meta_description', $metadescription , true);

First, make sure that get_the_content is returning the content you want and $meta_description isn't null.
Try This:
add_post_meta($post_id, 'meta_description', '' . $meta_description . '', true);

I found the best option, please try this.
global $post;
$metadescription = wp_trim_words( apply_filters('the_content', $post->post_content), 55, '');
add_post_meta($post_id, 'meta_description',$metadescription, true);

Related

WORDPRESS Get Filename to use as ALT

I have been using this code to set alt text to equal the title as the alt text is missing. Previously though I had used $attr['alt'] = $attr['title']; in order to use the filename as alt. It, however contains hyphens, so now I am trying to find a different way to get the filename.
I have read quite a few suggestions on how to retrieve the filename but so far nothing is working. Would also like to remove the file extension.
add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
function change_attachement_image_attributes($attr, $attachment) {
global $post;
$product = wc_get_product( $post->ID );
if ($post->post_type == 'product') {
$image_id = get_post_thumbnail_id();
$image_filename = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE);
$attr['alt'] = basename($image_filename);
}
return $attr;
}
How can I achieve this?
Assuming your current code works (I have not tested it) you can simply replace hyphens in title with spaces:
add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
function change_attachement_image_attributes($attr, $attachment) {
global $post;
$product = wc_get_product( $post->ID );
if ($post->post_type == 'product') {
$image_id = get_post_thumbnail_id();
$image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE);
$attr['alt'] = str_replace("-", " ", $attr['title']);
}
return $attr;
}

WordPress wp_insert_post function automatically converts the post_name value into lower case

I have a following code and it is so annoying that post_name is automatically converted to lowercase.
$post = array(
'post_name' => 'SampleWord',
),
);
// assume $post have other necessary fields as well
$post_id = wp_insert_post( $post, true );
When I check the post_name after above code it returns 'sampleword'.
I've checked the documentation of wp_insert_post and it mentioned 'post_name' field would be sanitized.
But converting to lowercase characters is not about sanitization.
And how to prevent it?
$arg = array(
'post_name' => 'SampleWord',
'post_title' => 'I Have CAPS',
);
// assume $post have other necessary fields as well
$post_id = wp_insert_post( $arg, true );
That's an easy one to confuse because of the names but post_name is really the slug and the documentation warns you it'll sanitize it (as you noted) but sanitize does also lowercase things.
Try the post_title addition above and see if it works out for you.
By default the post name is formatted and converted to lower-cased by the filter sanitize_title which hooks into sanitize_title_with_dashes(). As a solution you can replace the filter and omit the lower-case component.
add_filter( 'sanitize_title', 'custom_sanitize_title', 10, 3 );
function custom_sanitize_title( $title ) {
$title = strip_tags($title);
// Preserve escaped octets.
$title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
// Remove percent signs that are not part of an octet.
$title = str_replace('%', '', $title);
// Restore octets.
$title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
$title = remove_accents($title);
if (seems_utf8($title)) {
if (function_exists('mb_strtolower')) {
$title = mb_strtolower($title, 'UTF-8');
}
$title = utf8_uri_encode($title, 200);
}
// Prevent lower case
// $title = strtolower($title);
$title = preg_replace('/&.+?;/', '', $title); // kill entities
$title = str_replace('.', '-', $title);
$title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
$title = preg_replace('/\s+/', '-', $title);
$title = preg_replace('|-+|', '-', $title);
$title = trim($title, '-');
return $title;
}

I want to allow html tag when use the wp_trim_words

I want to show some content of post in anchor tag and show as link but I have use wp_trim_words function I don't know how to allow the html tags.
$more = ' Read More';
$content = wp_trim_words( get_the_content(), 50, $more );
echo do_shortcode($content);
Thanks to #dingo_d but he missed wpautop() in the code. Here is the updated code.
$more = ' Read More';
echo force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( wpautop(get_the_content()) ), 100, $more ) ) );
Aaaa I'm stupid. This will work (tested):
$more = ' Read More';
echo force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( get_the_content() ), 50, $more ) ) );
It took me a while to remember that wp_trim_words() strip all tags from the content it has, so your HTML wasn't displayed. So I did a little digging and found this gem:
Wordpress track ticket #29533.
Hope this helps. Sorry for the confusion before :)

Change the output of something using Add_filter in WordPress

I am using Tim Whitlock's Latest Tweets plugin to display tweets.
I would like to modify the plugin's default output. I have found the below in line 137 of the plugin file:
$final = apply_filters('latest_tweets_render_tweet', $html, $date, $link, $tweet );
if( $final === $html ){
$final = '<p class="tweet-text">'.$html.'</p>'.
'<p class="tweet-details">'.$date.'</p>';
}
$rendered[] = $final;
And have come up with this filter to attempt to change the output:
add_filter('latest_tweets_render_tweet', 'modified_tweets', 10);
function modified_tweets( $html, $date, $link, $tweet ) {
$final = '<div class="col-sm-4">'.
'<p class="tweet-text2">'.$html.'</p>'.
'<p class="tweet-details">'.$date.'</p>'.
'</div>';
$rendered[] = $final;
};
However, the output remains the same. Any ideas what I'm doing wrong?
You have to return that array, so add:-
return $rendered;
after this:-
$rendered[] = $final;
As DWX mentioned, you're missing the return. But you don't need to return an array, the original code is expecting a string:
$my_final = '<div class="col-sm-4">'. $etcetera;
return $my_final;
And also add the number of arguments after the priority when adding the filter:
add_filter('latest_tweets_render_tweet', 'modified_tweets', 10, 4);

wp_trim_words not working on anything but ordinary strings

I´m a bit out of my field here and I´m confused about this. Using wp_trim_field doesn't work for me except for regular strings.
This does not work, it returns the whole text:
<?php
$field = the_field('project_description');
$trimmedfield = wp_trim_words( $field, $num_words = 1, $more = '… ' );
echo '<p>' . $trimmedfield . '</p>';
?>
This however does work:
<?php
$field = 'this text does get trimmed';
$trimmedfield = wp_trim_words( $field, $num_words = 1, $more = '… ' );
echo '<p>' . $trimmedfield . '</p>';
?>
Echoing out the $field instead does echo out the text that I am trying to trim, but the trimming aint working. Any ideas as to why?
edit - I also tried this, same thing happens:
<?php
$length = 1;
$text = the_field('project_description');
$words = explode(' ', $text);
array_splice($words, $length);
$text = implode(' ', $words);
echo $text;
?>
use var_dump($field); wp_trim_words( $field,....) $field must be string type ...check if this is or not to test the datatype, if its not im sure you know what to do then.
Use typecast if its not.
You'll need to change the $field variable to this: $field = get_field('project_description');
the_field(); outputs the content, while get_field(); retrieves it. In order to pass it through a function, you'll need to retrieve it.
ACF documentation page that answers this question: https://www.advancedcustomfields.com/resources/displaying-custom-field-values-in-your-theme/

Resources