{"id":21486,"date":"2014-06-23T18:01:11","date_gmt":"2014-06-23T10:01:11","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=21486"},"modified":"2025-04-04T01:58:37","modified_gmt":"2025-04-03T17:58:37","slug":"build-a-wordpress-plugin-to-restrict-content-to-registered-user","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/","title":{"rendered":"How to Restrict Content to Registered WordPress Users"},"content":{"rendered":"<p>In recent times, most online news and information publication websites have adopted the freemium model whereby readers who aren\u2019t a registered member are <strong>limited to certain number of article<\/strong>s they can read; paying, registered users on the other hand, <strong>have unlimited access to articles.<\/strong><\/p>\n<p>In this article, we will be showing you how to build a simple plugin that <strong>gives the administrator of a WordPress-powered site the ability to restrict certain posts, pages and part<\/strong> of a post content to registered users only.<\/p>\n<div class=\"ref-block ref-block--post\" id=\"ref-post-1\">\n\t\t\t\t\t<a href=\"https:\/\/www.hongkiat.com\/blog\/beginners-guide-to-wordpress-plugin-development\/\" class=\"ref-block__link\" title=\"Read More: Beginner\u2019s Guide to WordPress Plugin Development\" rel=\"bookmark\"><span class=\"screen-reader-text\">Beginner\u2019s Guide to WordPress Plugin Development<\/span><\/a>\n<div class=\"ref-block__thumbnail img-thumb img-thumb--jumbo\" data-img='{ \"src\" : \"https:\/\/assets.hongkiat.com\/uploads\/thumbs\/250x160\/beginners-guide-to-wordpress-plugin-development.jpg\" }'>\n\t\t\t\t\t\t\t<noscript>\n<style>.no-js #ref-block-post-9592 .ref-block__thumbnail { background-image: url(\"https:\/\/assets.hongkiat.com\/uploads\/thumbs\/250x160\/beginners-guide-to-wordpress-plugin-development.jpg\"); }<\/style>\n<\/noscript>\n\t\t\t\t\t\t<\/div>\n<div class=\"ref-block__summary\">\n<h4 class=\"ref-title\">Beginner\u2019s Guide to WordPress Plugin Development<\/h4>\n<p class=\"ref-description\">\n\t\t\t\t\t\tThe WordPress CMS has changed the face of our Internet and allowed a surge of new ideas to...\t\t\t\t\t\t<span>Read more<\/span><\/p>\n<\/div>\n<\/div>\n<h2>Coding the Plugin<\/h2>\n<p>When writing a WordPress plugin, the header (a PHP comment block) section contains information such as name, description, author and author URL of the plugin.\n  Here is the plugin header:<\/p>\n<pre>&lt;?php\r\n\/*\r\nPlugin Name: Restrict Content to registered users\r\nPlugin URI: http:\/\/hongkiat.com\r\nDescription: Restricting content to registered users only\r\nVersion: 0.1\r\nAuthor: Agbonghama Collins\r\nAuthor URI: http:\/\/tech4sky.com\r\n*\/\r\n<\/pre>\n<p>The plugin will have a settings page consisting of a form field which will contain the post or page IDs to be restricted.<\/p>\n<p>The code below will add a sub-menu to the <strong>Settings<\/strong> titled <code>Restrict content To Registered User<\/code>.<\/p>\n<pre>add_action('admin_menu', 'rcru_plugin_menu');\r\n\/\/ Adding Submenu to settings\r\nfunction rcru_plugin_menu() {\r\n    add_options_page(\r\n        'Restrict content To Registered User',\r\n        'Restrict content To Registered User',\r\n        'manage_options',\r\n        'rcru-restrict-content-user',\r\n        'rcru_content_user_settings'\r\n    );\r\n}\r\n<\/pre>\n<p>The fifth argument <code>rcru_content_user_settings<\/code> passed to <code>add_options_page<\/code> above is the function that will output the content for the plugin settings.<\/p>\n<pre>\r\nfunction rcru_content_user_settings() {\r\n    echo '&lt;div class=\"wrap\"&gt;';\r\n    screen_icon();\r\n    echo '&lt;h2&gt;Restrict content To Registered User&lt;\/h2&gt;';\r\n    echo '&lt;form action=\"options.php\" method=\"post\"&gt;';\r\n    do_settings_sections('rcru-restrict-content-user');\r\n    settings_fields('rcru_settings_group');\r\n    submit_button();\r\n}\r\n<\/pre>\n<p>The form lacks the <code>&lt;input&gt;<\/code> field and it isn\u2019t capable just yet to save data to the database because we have yet to implement the WordPress <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/codex.wordpress.org\/Settings_API\">settings API<\/a>.<\/p>\n<p>The function <code>plugin_option<\/code> defines the settings section and field.<\/p>\n<pre>\r\n\/\/ plugin field and sections\r\nfunction plugin_option() {\r\n    add_settings_section(\r\n        'rcru_settings_section',\r\n        'Plugin Options',\r\n        null,\r\n        'rcru-restrict-content-user'\r\n    );\r\n    \r\n    add_settings_field(\r\n        'post-page-id',\r\n        '&lt;label for=\"post-page-id\"&gt;Post and page ID to be restricted.&lt;\/label&gt;',\r\n        'post_page_field',\r\n        'rcru-restrict-content-user',\r\n        'rcru_settings_section'\r\n    );\r\n    \r\n    \/\/ register settings\r\n    register_setting('rcru_settings_group', 'rcru_post-id-option');\r\n}\r\n<\/pre>\n<p>Mind you, the third argument <code>post_page_field<\/code> passed to the <code>add_settings_field<\/code> function above is called to echo the form <code>&lt;input&gt;<\/code> field.<\/p>\n<pre>function post_page_field() {\r\n    echo \"Enter Post or Page ID separated by comma. &lt;br\/&gt;\";\r\n    echo '&lt;input style=\"width: 300px; height:80px\" type=\"text\" id=\"post-page-id\" name=\"rcru_post-id-option\" value=\"' . get_option('rcru_post-id-option') . '\"&gt;';\r\n}\r\n<\/pre>\n<p>The function <code>plugin_option<\/code> is finally hooked to the <code>admin_init<\/code> action to put it into action.<\/p>\n<pre>\r\nadd_action('admin_init', 'plugin_option');\r\n<\/pre>\n<p>We are done with building the plugin settings page but what use is the data saved by the settings page to the database if it isn\u2019t going to be used?<\/p>\n<p>Next is the coding of the function <code>restrict_content_register_user<\/code> that will retrieve the post or page ID to be restricted to only registered users (which was saved to the database in the plugin settings page). This ensures the current user viewing the post is registered; otherwise an error message telling the user to register would be displayed.<\/p>\n<p>Finally, the function will be hooked to the_content filter so as to affect the change in the post or page.<\/p>\n<pre>function restrict_content_register_user($content) {\r\n    global $post;\r\n    $post_database = get_option('rcru_post-id-option');\r\n    $post_database = explode(',', $post_database);\r\n    $current_user = wp_get_current_user();\r\n    \r\n    if (is_null($content))\r\n        return $content;\r\n    \r\n    foreach ($post_database as $posts) {\r\n        $posts = trim($posts);\r\n        if ($posts == $post -&gt; ID) {\r\n            if (username_exists($current_user -&gt; user_login)) {\r\n                \/* Return the private content. *\/\r\n                return $content;\r\n            } else {\r\n                \/* Return an alternate message. *\/\r\n                return '&lt;div align=\"center\" style=\"color: #fff; padding: 20px; border: 1px solid border-color: rgb(221, 204, 119); background-color: #3B5998\"&gt;\r\n                    You must be a registered user to read this content.\r\n                    &lt;br\/&gt;\r\n                    &lt;a style=\"font-size: 20px;\" href=\"' . get_site_url() . '\/wp-login.php?action=register\"&gt;Register Here&lt;\/a&gt;\r\n                &lt;\/div&gt;';\r\n            }\r\n        }\r\n    }\r\n    return $content;\r\n}\r\nadd_filter('the_content', 'restrict_content_register_user');\r\n<\/pre>\n<p>We are now done with the first way the plugin work: the use of the plugin settings page.<\/p>\n<p>What\u2019s left is to <strong>add the metabox<\/strong> and <strong>shortcode feature<\/strong> to the plugin.<\/p>\n<h2>Adding Metabox<\/h2>\n<p>To add a metabox containing a checkbox in post and page edit screens will allow restriction of that post or page to registered user when the checkbox is ticked. The function <code>rcru_mb_create<\/code> will include the meta box in all post and pages when hooked to the <code>add_meta_boxes<\/code> action.<\/p>\n<pre>\r\nfunction rcru_mb_create() {\r\n    \/**\r\n     * @array $screens Write screen on which to show the meta box\r\n     * @values post, page, dashboard, link, attachment, custom_post_type\r\n     *\/\r\n    $screens = array(\r\n        'post',\r\n        'page'\r\n    );\r\n    foreach ($screens as $screen) {\r\n        add_meta_box('rcru-meta',\r\n            'Restrict Post \/ Page',\r\n            'rcru_mb_function',\r\n            $screen,\r\n            'normal',\r\n            'high');\r\n    }\r\n}\r\nadd_action('add_meta_boxes', 'rcru_mb_create');\r\n<\/pre>\n<p>The function <code>rcru_mb_function<\/code> contains the checkbox and description of the metabox.<\/p>\n<pre>\r\nfunction rcru_mb_function($post) {\r\n    \r\n    \/\/retrieve the metadata values if they exist\r\n    $restrict_post = get_post_meta($post -&gt; ID, '_rcru_restrict_content', true);\r\n    \r\n    \/\/ Add an nonce field so we can check for it later when validating\r\n    wp_nonce_field('rcru_inner_custom_box', 'rcru_inner_custom_box_nonce');\r\n    \r\n    echo '&lt;div style=\"margin: 10px 100px; text-align: center\"&gt;\r\n        &lt;table&gt;\r\n            &lt;tr&gt;\r\n                &lt;th scope=\"row\"&gt;&lt;label for=\"rcru-restrict-content\"&gt;Restrict content?&lt;\/label&gt;&lt;\/th&gt;\r\n                &lt;td&gt;\r\n                    &lt;input type=\"checkbox\" value=\"1\" name=\"rcru_restrict_content\" id=\"rcru-restrict-content\"' . checked($restrict_post, 1, false) . '&gt;\r\n                    &lt;span class=\"description\"&gt;Checking this setting will restrict this post to only registered users.&lt;\/span&gt;\r\n                &lt;\/td&gt;\r\n            &lt;\/tr&gt;\r\n        &lt;\/table&gt;\r\n    &lt;\/div&gt;';\r\n}\r\n<\/pre>\n<p>The <code>rcru_mb_save_data<\/code> function handles the security check and the saving of the form values to the database.<\/p>\n<pre>\r\nfunction rcru_mb_save_data($post_id) {\r\n    \r\n    \/\/ Check if our nonce is set.\r\n    if (!isset($_POST['rcru_inner_custom_box_nonce']))\r\n        return $post_id;\r\n    \r\n    $nonce = $_POST['rcru_inner_custom_box_nonce'];\r\n    \r\n    \/\/ Verify that the nonce is valid.\r\n    if (!wp_verify_nonce($nonce, 'rcru_inner_custom_box'))\r\n        return $post_id;\r\n    \r\n    \/\/ If this is an autosave, our form has not been submitted, so we don't want to do anything.\r\n    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)\r\n        return $post_id;\r\n    \r\n    \/\/ Check the user's permissions.\r\n    if ('page' == $_POST['post_type']) {\r\n        \r\n        if (!current_user_can('edit_page', $post_id))\r\n            return $post_id;\r\n    } else {\r\n        \r\n        if (!current_user_can('edit_post', $post_id))\r\n            return $post_id;\r\n    }\r\n    \r\n    \/* OK, its safe for us to save the data now. *\/\r\n    \r\n    \/\/ If old entries exist, retrieve them\r\n    $old_restrict_post = get_post_meta($post_id, '_rcru_restrict_content', true);\r\n    \r\n    \/\/ Sanitize user input.\r\n    $restrict_post = sanitize_text_field($_POST['rcru_restrict_content']);\r\n    \r\n    \/\/ Update the meta field in the database.\r\n    update_post_meta($post_id, '_rcru_restrict_content', $restrict_post, $old_restrict_post);\r\n    \r\n}\r\n\r\n\/\/hook to save the meta box data\r\nadd_action('save_post', 'rcru_mb_save_data');\r\n<\/pre>\n<p>The function <code>restrict_content_metabox<\/code> will examine a given post or page to see if it has restrictions, check if the user reading the post is registered and display a notice telling the user to register.<\/p>\n<pre>\r\nfunction restrict_content_metabox($content) {\r\n    global $post;\r\n    \/\/retrieve the metadata values if they exist\r\n    $post_restricted = get_post_meta($post -&gt; ID, '_rcru_restrict_content', true);\r\n    \r\n    \/\/ if the post or page has restriction and the user isn't registered\r\n    \/\/ display the error notice\r\n    if ($post_restricted == 1 && (!username_exists(wp_get_current_user()-&gt;user_login)) ) {\r\n        \r\n        return '&lt;div align=\"center\" style=\"color: #fff; padding: 20px; border: 1px solid #ddd; background-color: #3B5998\"&gt;\r\n            You must be a registered user to read this content.\r\n            &lt;br\/&gt;\r\n            &lt;a style=\"font-size: 20px;\" href=\"' . get_site_url() . '\/wp-login.php?action=register\"&gt;Register Here&lt;\/a&gt;\r\n        &lt;\/div&gt;';\r\n    }\r\n    \r\n    return $content;\r\n    \r\n}\r\n\r\n\/\/ hook the function to the post content to effect the change\r\nadd_filter('the_content', 'restrict_content_metabox');\r\n<\/pre>\n<h2>Adding the Shortcode<\/h2>\n<p>With the shortcode, part of a post can be restricted to only registered users.<\/p>\n<p>The function <code>rcru_user_shortcodes<\/code> contains the shortcode hook function <code>add_shortcode<\/code> that defines the shortcode tag <code>[rcru-private]<\/code>.\n  The second argument passed to <code>add_shortcode<\/code> is the callback function that is called when the shortcode is in use.<\/p>\n<pre>\r\n\/* Function for registering the shortcode. *\/\r\nfunction rcru_user_shortcodes() {\r\n    \r\n    \/* Adds the [rcru-private] shortcode. *\/\r\n    add_shortcode('rcru-private', 'rcru_shortcode');\r\n}\r\n<\/pre>\n<p>The function is then registered to <code>init<\/code> action so it will be recognised by WordPress internals.<\/p>\n<pre>\r\n\/* Register shortcodes in 'init'. *\/\r\nadd_action('init', 'rcru_user_shortcodes');\r\n<\/pre>\n<p>Finally, the <code>rcru_shortcode<\/code> callback function handles the shortcode output.<\/p>\n<pre>\r\n\/* Function for handling shortcode output. *\/\r\nfunction rcru_shortcode($attr, $content = '') {\r\n    \r\n    \/* Check if the current user has the 'read_private_content' capability. *\/\r\n    $current_reader = wp_get_current_user();\r\n    if (!username_exists($current_reader -&gt; user_login)) {\r\n        \r\n        \/* Return an alternate message. *\/\r\n        return '&lt;div align=\"center\" style=\"color: #fff; padding: 20px; border: 1px solid border-color: rgb(221, 204, 119); background-color: #3B5998\"&gt;\r\n            You must be a registered user to read this content.\r\n            &lt;br\/&gt;\r\n            &lt;a style=\"font-size: 20px;\" href=\"' . get_site_url() . '\/wp-login.php?action=register\"&gt;Register Here&lt;\/a&gt;\r\n        &lt;\/div&gt;';\r\n    }\r\n}\r\n<\/pre>\n<h2>How the plugin works<\/h2>\n<p>The plugin will have a settings page with a form field that accept Post & Page IDs to be restricted, delimited by a comma.<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/wordpress-plugin-restrict-content-registered-users\/restrict-post-metabox.jpg\" height=\"91\" width=\"600\" alt=\"\"><\/figure>\n<p>To restrict a given post or page, enter their respective IDs, separated by a comma (,) in the<strong> <\/strong>form field. To get the ID of a post, go to the post edit screen (TinyMCE editor for writing post content), and look at the URL of the page. The number appended to <code>?post=<\/code> is the post ID.<\/p>\n<p>For instance, for <code>http:\/\/wordpress.dev\/wp-admin\/post.php?post=88&action=edit<\/code>, the number <strong>88<\/strong> is the post or page ID.<\/p>\n<h2>Metabox in post & Page edit screen<\/h2>\n<p>A post or page can also be restricted to registered users by ticking the <strong>Restrict content<\/strong> checkbox located at the metabox (added by the plugin to the post <strong>edit screen<\/strong>).<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/wordpress-plugin-restrict-content-registered-users\/restrict-post-metabox.jpg\" height=\"91\" width=\"600\" alt=\"\"><\/figure>\n<h2>Shortcode<\/h2>\n<p>Part or the section of a post or page content can be hidden from public view and displayed only to registered members with the use of a plugin shortcode like this:<\/p>\n<pre>[rcru-private]Part of post or page content to be restricted to only registered users.[\/rcru-private]\r\n <\/pre>\n<p>Lastly, here is a link to the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/assets.hongkiat.com\/uploads\/wordpress-plugin-restrict-content-registered-users\/restrict-content-registered-users.zip\">plugin file<\/a>. Feel free to explore the code and happy coding!<\/p>","protected":false},"excerpt":{"rendered":"<p>In recent times, most online news and information publication websites have adopted the freemium model whereby readers who aren\u2019t a registered member are limited to certain number of articles they can read; paying, registered users on the other hand, have unlimited access to articles. In this article, we will be showing you how to build&hellip;<\/p>\n","protected":false},"author":422,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[49],"tags":[4663],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.6) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Restrict Content to Registered WordPress Users - Hongkiat<\/title>\n<meta name=\"description\" content=\"In recent times, most online news and information publication websites have adopted the freemium model whereby readers who aren&#039;t a registered member are\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Restrict Content to Registered WordPress Users\" \/>\n<meta property=\"og:description\" content=\"In recent times, most online news and information publication websites have adopted the freemium model whereby readers who aren&#039;t a registered member are\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2014-06-23T10:01:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-03T17:58:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/assets.hongkiat.com\/uploads\/wordpress-plugin-restrict-content-registered-users\/restrict-post-metabox.jpg\" \/>\n<meta name=\"author\" content=\"Collins Agbonghama\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Collins Agbonghama\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\\\/\"},\"author\":{\"name\":\"Collins Agbonghama\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/a74918198fb6afb83c7eecb598932be8\"},\"headline\":\"How to Restrict Content to Registered WordPress Users\",\"datePublished\":\"2014-06-23T10:01:11+00:00\",\"dateModified\":\"2025-04-03T17:58:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\\\/\"},\"wordCount\":744,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/wordpress-plugin-restrict-content-registered-users\\\/restrict-post-metabox.jpg\",\"keywords\":[\"ad-divi\"],\"articleSection\":[\"WordPress\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\\\/\",\"name\":\"How to Restrict Content to Registered WordPress Users - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/wordpress-plugin-restrict-content-registered-users\\\/restrict-post-metabox.jpg\",\"datePublished\":\"2014-06-23T10:01:11+00:00\",\"dateModified\":\"2025-04-03T17:58:37+00:00\",\"description\":\"In recent times, most online news and information publication websites have adopted the freemium model whereby readers who aren't a registered member are\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\\\/#primaryimage\",\"url\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/wordpress-plugin-restrict-content-registered-users\\\/restrict-post-metabox.jpg\",\"contentUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/wordpress-plugin-restrict-content-registered-users\\\/restrict-post-metabox.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Restrict Content to Registered WordPress Users\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/a74918198fb6afb83c7eecb598932be8\",\"name\":\"Collins Agbonghama\",\"description\":\"Agbonghama is a web developer by day and freelance writer\\\/blogger by night. When not wrangling with code, he is fond of sleeping on the couch and writing on his personal blog w3guy.com.\",\"sameAs\":[\"https:\\\/\\\/w3guy.com\\\/\"],\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/collinsagbonghama\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Restrict Content to Registered WordPress Users - Hongkiat","description":"In recent times, most online news and information publication websites have adopted the freemium model whereby readers who aren't a registered member are","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/","og_locale":"en_US","og_type":"article","og_title":"How to Restrict Content to Registered WordPress Users","og_description":"In recent times, most online news and information publication websites have adopted the freemium model whereby readers who aren't a registered member are","og_url":"https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2014-06-23T10:01:11+00:00","article_modified_time":"2025-04-03T17:58:37+00:00","og_image":[{"url":"https:\/\/assets.hongkiat.com\/uploads\/wordpress-plugin-restrict-content-registered-users\/restrict-post-metabox.jpg","type":"","width":"","height":""}],"author":"Collins Agbonghama","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Collins Agbonghama","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/"},"author":{"name":"Collins Agbonghama","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/a74918198fb6afb83c7eecb598932be8"},"headline":"How to Restrict Content to Registered WordPress Users","datePublished":"2014-06-23T10:01:11+00:00","dateModified":"2025-04-03T17:58:37+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/"},"wordCount":744,"commentCount":3,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/wordpress-plugin-restrict-content-registered-users\/restrict-post-metabox.jpg","keywords":["ad-divi"],"articleSection":["WordPress"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/","url":"https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/","name":"How to Restrict Content to Registered WordPress Users - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/#primaryimage"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/wordpress-plugin-restrict-content-registered-users\/restrict-post-metabox.jpg","datePublished":"2014-06-23T10:01:11+00:00","dateModified":"2025-04-03T17:58:37+00:00","description":"In recent times, most online news and information publication websites have adopted the freemium model whereby readers who aren't a registered member are","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/#primaryimage","url":"https:\/\/assets.hongkiat.com\/uploads\/wordpress-plugin-restrict-content-registered-users\/restrict-post-metabox.jpg","contentUrl":"https:\/\/assets.hongkiat.com\/uploads\/wordpress-plugin-restrict-content-registered-users\/restrict-post-metabox.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/build-a-wordpress-plugin-to-restrict-content-to-registered-user\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Restrict Content to Registered WordPress Users"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/a74918198fb6afb83c7eecb598932be8","name":"Collins Agbonghama","description":"Agbonghama is a web developer by day and freelance writer\/blogger by night. When not wrangling with code, he is fond of sleeping on the couch and writing on his personal blog w3guy.com.","sameAs":["https:\/\/w3guy.com\/"],"url":"https:\/\/www.hongkiat.com\/blog\/author\/collinsagbonghama\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-5Ay","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/21486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/422"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=21486"}],"version-history":[{"count":3,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/21486\/revisions"}],"predecessor-version":[{"id":73702,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/21486\/revisions\/73702"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=21486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=21486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=21486"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=21486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}