Ask questions about WordPress courses

Hiding Part of WordPress Content with a Shortcode

Website development, one of the main tasks, facing developers, is to attract new users in order to offer them new interesting lessons, articles or services.
Today I will tell you one of the popular ways to attract new visitors to register.. Sometimes when adding a new article to the site, you can simply hide the link or part of the content. At the same time, displaying a message asking you to register. First, paste the following code into function.php

Code Example:

add_shortcode( 'access', 'access_check_shortcode' );
function access_check_shortcode( $attr, $content = null ) {
	extract( shortcode_atts( array( 'capability' => 'read' ), $attr ) );
	if ( current_user_can( $capability ) && !is_null( $content ) && !is_feed() ){
		return $content;
	}else{
		return 'Часть статьи была скрыта. Для просмотра подалуйста зарегистрируйтесь.';
	}
}

To hide, add the code to the text of the article:

[access capability="switch_themes"]
And here is the hidden text itself.
[/access]

And there is no need to use plugins.. Everything works flexibly and well.
Here I fantasized a little and did it my way )

[access capability=”switch_themes”] And here is the hidden text itself.
[/access]

 

In code fragments, it is easy to change individual parts of the design and supplement them. This is not always available in plugins., and sometimes even for a fee. This is what makes working with plugins and code snippets different.. So learn and develop your own. Good luck with development!

 

Consider one more code. More precisely, a shortcode that hides part of the page until a certain date in WordPress.
The code can be useful if you want to hide part of the text for a while, e.g. for promotions. Can also be used to count days, until the new Year:

function content_countdown($atts, $content = null){
  extract(shortcode_atts(array(
     'month' => '',
     'day'   => '',
     'year'  => ''
    ), $atts));
    $remain = ceil((mktime( 0,0,0,(int)$month,(int)$day,(int)$year) - time())/86400);
    if( $remain > 1 ){
        return $daysremain = "
Just ($remain) days until content is available
";
    }else if($remain == 1 ){
	return $daysremain = "
Just ($remain) day until content is available
";
    }else{
        return $content;
    }
}
add_shortcode('cdt', 'content_countdown');

Sample code to hide part of the content on the site until a certain date:

[cdt month="12" day="17" year="2012"]
This is content that will only be shown after a set number of days.
[/cdt]

After inserting the shortcode, the line will be displayed:
Just (54) days until content is available


Купить хостинг WordPress
/* WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

Nikolaenko Maxim

Director of web studies ProGrafika. I am developing, website design and promotion. Always glad to new blog readers and good clients.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Templates for WordPress
The best hosting in Ukraine
Stable hosting for Drupal