Hide after X characters in php? - php

I have a Wordpress site, and I want to set up a paywall for certain content.
I would like to show some intro of each such article for Google index and users as well, but rest of it would be hidden for guests and available for logged in (in my case paid users).
I successfully implemented is_user_logged_in() PHP statement from Wordpress, but I am not finding PHP code anywhere online about hiding everything after n-characters, or words.
My intended workflow is:
<?php
if ( is_user_logged_in() ) {
the_content();
} else {
echo 'show only 200 characters or words of the content code should be here';
}
?>

You can use WordPress default function wp_trim_words.
<?php
if ( is_user_logged_in() ) {
the_content();
} else {
$content = get_the_content(); // $content is whatever your content field.
echo wp_trim_words( $content, 200, ' ' );
}
?>

<?php
if ( is_user_logged_in() ) {
the_content();
} else {
$content = get_the_content();
//If your template has some other way of getting the post content then use that variable.
echo substr($content, 0, strpos($content, ' ', 200));
}
?>

Related

How to apply excerpt inside my XML data on WordPress

I'm pulling out the data from my CRM to my WordPress site using a XML.
It worked actually, but I want to add some functions like read more with the my XML data {Web_Remarks[1]} it contains a long description.
Then I tried to add conditions from from my function.php
like excerpt and edit my descriptions code.
For my functions.php I add
<?php
// Customize excerpt for description word count length
function custom_excerpt_length(){
return 25;
}
add_filter('excerpt_length','custom_excerpt_length');
?>
And for my description.php I change my <?php the_content();?> to
<?php if ($post->post_excerpt) { ?>
<p>
<?php echo get_the_excerpt(); ?>
Read MoreĀ»
</p>
<?php }else{
the_content();
}
?>
I expect the output should be "3Bed room with laundry ro..Read More..."
but what I have now is
"3Bed room with laundry room and sea view with 2 car parkings only for 3.06M!
-Bright
-Spacious
-Sea/Palm view
-Balconies
-Higher floor
-No Construction chance infront
-Prime location
real estate are a Property Investment Firm with an ownership of more than 2500 properties all around the world.These are handpicked exclusive apartments and villas, located within the most prestigious and high-profile developments of Dubai. We do not just believe in customer satisfaction, we aim for customer delight. We understand that our customers define the standard of quality and service and your loyalty must be earned."
Try,
<?php
$excerpt = get_the_excerpt();
if( !empty($excerpt) ) { ?>
<p>
<?php echo $excerpt; ?>
Read MoreĀ»
</p>
<?php
} else {
the_content();
}
Or this will be a another method you can use:
<?php
// Add this code in functions.php file
function wp_get_custom_excerpt( $limit = '25' ) {
global $post;
$content = get_the_excerpt();
if( empty($content) ) {
$content = strip_shortcodes( $post->post_content );
}
$excerpt = wp_trim_words( $content, $limit, '[...]' );
return $excerpt;
}
// Remove your excerpt code and just echo function.
echo wp_get_custom_excerpt();

Get the whole page content and display in the header

Created a page and want to display all the content on the header.php, but it does not display anything.
code below:
$page = get_page_by_title( 'TEST' );
$content = apply_filters('the_content', $page->post_content);
echo $content;
Is there another way to do it?
Try to do this (just replace 123 with your page ID):
$page_object = get_page( 123 );
$my_content=$page_object->post_content;
echo do_shortcode( $my_content );
Use following code to get:
$page = get_page_by_title('TEST', OBJECT, 'page');
echo $page->post_content;
Use following code if there is shortcode added in page and text added in the page assume Contact page:
$page = get_page_by_title('Contact', OBJECT, 'page');
$page->post_content;
// will match square brackets
if (preg_match('^\[(.*?)\]^', $page->post_content))
{
echo do_shortcode($page->post_content);
}else{
echo $page->post_content;
}
Use if you have text as well as shortcode in the page replace 226 with your page id;
$id=226;
$post = get_page($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
Read: https://codex.wordpress.org/Function_Reference/get_page_by_title

Show excerpt on front page using the_content(); and ACF

I need to show a excerpt on my home page. I have standard post and a custom post type 'webinar'. The CPT 'webinar' has a custom field, using ACF 'webinar_description' that is a an equivalent of 'description' in normal posts.
I am able to display both, by adding a filter for the 'webinar_description' like this:
add_filter('the_content','add_my_custom_field');
function add_my_custom_field($data) {
$data .= get_field('webinar_description');
return $data;
}
Now I have both post types displaying, but it displays the entire 'description' or 'webinar_description' field. I need to trim it at 40 words and add a '... Read More' with 'Read More' being a link to the article.
I have tried this, but it only works on the normal 'post' type field 'description' it doesn't work on my 'webinar' custom post type -> custom field 'webinar-description'
<?php $content = get_the_content(); echo mb_strimwidth($content, 0, 400, '[Read more]');?>
How can I create a filter or function that will limit both to 400 (or whatever) characters and add the link?
Not sure if this will help anyone in a similar situation, but this is how I solved it. First, in functions.php make the custom post type available
function cpt_get_excerpt(){
$excerpt = get_field('webinar_description');
$excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, 400);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
$excerpt = $excerpt.'... <a class="c-drkGold" href="'.get_permalink($post->ID).'">Read More</a>';
return $excerpt;
}
Then where you want to display it, use a if/else statement based on the post type and display accordingly (this example is from static front page).
<?php
if( get_post_type() == 'post' ) {
?><p class="l-nmb"><?php
$content = get_the_content(); echo mb_strimwidth($content, 0, 400, '... Read more'); ?> </p>
<?php } else {
?><p class="l-nmb"><?php
$content = cpt_get_excerpt(); echo mb_strimwidth($content, 0, 400, '... Read more'); ?> </p>
<?php } ?>

Thumbnail to external link AND Thumbnail to post

Here is what I'd like to accomplish
Goal A: I want to hyperlink each thumbnail in index.php to their post.
Goal B: I want to define a hyperlink for each thumbnail in single.php to an external website.
You may ask why am I using thumbnails for single.php? The reason is because I want this layout:
And so far I understand that there are 3 methods to display images:
Insert image into the editor area along with the text, but the problem is I cannot float the image and text differently because all items within a post are assigned a p tag - am I wrong?
Custom fields should get the job done but it doesn't seem the most efficient way - or am I wrong?
Post Thumbnails should be the easiest way but see my problem below
I have the code to accomplish Goal A and B but they only work separately.
In other words, "Code 1" does not work if "Code 2" is present.
How can I resolve this issue? Or is there a better method accomplish my goal?
Code 1: Link thumbnails to external websites using custom field (single.php)
<?php $name = get_post_meta($post->ID, 'externalurl', true);
if( $name ) { ?>
<?php the_post_thumbnail(); ?>
<?php } else {
the_post_thumbnail();
} ?>
Code 2: Link thumbnails to the post (functions.php)
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
function my_post_image_html( $html, $post_id, $post_image_id ) {
$html = '' . $html . '';
return $html;
}
is_single() function will help you achieve what you need. Try below code in functions.php and remove the additional code from single.php
function my_post_image_html( $html, $post_id, $post_image_id ) {
if ( is_single()) {
$name = get_post_meta($post_id, 'externalurl', true);
if( $name ) {
$html = '' . $html . '';
}
return $html;
}
else
{
$html = '' . $html . '';
return $html;
}
}
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );

Placing PHP code in social locker PHP shortcode

I am using wordpress. I am using the plugin social locker.
I have figured out how to display the shortcode via PHP:
<?php echo do_shortcode('[sociallocker id="3071"] My content Here [/sociallocker]'); ?>
However I want to display this button that will allow for a print popup in between there.
This code is:
<?php if ( function_exists( 'pdf_print_popup' ) ) pdf_print_popup(); ?>
So my question is can I do this:
<?php echo do_shortcode('[sociallocker id="3071"] <?php if ( function_exists( 'pdf_print_popup' ) ) pdf_print_popup(); ?> [/sociallocker]'); ?>
You have opening / closing PHP tags inside your echo statement.
Try this instead:
<?php
ob_start();
if ( function_exists( 'pdf_print_popup' ) ) {
pdf_print_popup();
}
$content = ob_get_clean();
echo do_shortcode( '[sociallocker id="3071"]' . $content . '[/sociallocker]' ); ?>
You'd be better modifying pdf_print_popup to return instead of output a value as well removing the need for output buffering.

Categories