{"id":45717,"date":"2022-07-08T14:49:11","date_gmt":"2022-07-08T14:49:11","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=45717"},"modified":"2026-10-10T18:35:42","modified_gmt":"2026-10-10T18:35:42","slug":"custom-password-protected-message","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/custom-password-protected-message\/","title":{"rendered":"Change the WordPress Password Protected Message"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When you password protect a post or page in WordPress, visitors see a fixed message above the password field. This snippet changes the WordPress password protected message to your own text, can add a link where people request the password, or removes the message completely.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It works whatever language the site is in and whichever wording your WordPress version uses, because it does not search for the English sentence.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Change the message above the WordPress password form on protected posts and pages.\n *\/\n\n\/\/ ===== Config =====\nfunction dpc_password_message_config() {\n\t$config = array(\n\t\t\/\/ Text above the password field. Empty '' (and no link) removes the paragraph.\n\t\t'message'   =&gt; __( 'This content is password protected. Enter the password below, or request one if you do not have it.', 'dpc' ),\n\t\t\/\/ Optional link after the text, e.g. your contact page: home_url( '\/request-password\/' ). Empty '' = no link.\n\t\t'link_url'  =&gt; '',\n\t\t'link_text' =&gt; __( 'Request a password', 'dpc' ),\n\t);\n\t\/\/ ===== End of config =====\n\n\treturn (array) apply_filters( 'dpc_password_message_config', $config );\n}\n\nadd_filter( 'the_password_form', 'dpc_password_message', 10, 2 );\n\nfunction dpc_password_message( $output, $post = null ) {\n\t$config = wp_parse_args(\n\t\tdpc_password_message_config(),\n\t\tarray( 'message' =&gt; '', 'link_url' =&gt; '', 'link_text' =&gt; '' )\n\t);\n\n\t$html = esc_html( $config&#91;'message'&#93; );\n\t$url  = esc_url( $config&#91;'link_url'&#93; );\n\tif ( '' !== $url &amp;&amp; '' !== $config&#91;'link_text'&#93; ) {\n\t\t$html .= ( '' === $html ? '' : ' ' ) . sprintf(\n\t\t\t'&lt;a class=\"dpc-password-request\" href=\"%1$s\"&gt;%2$s&lt;\/a&gt;',\n\t\t\t$url,\n\t\t\tesc_html( $config&#91;'link_text'&#93; )\n\t\t);\n\t}\n\t$html = (string) apply_filters( 'dpc_password_message_html', $html, $post );\n\n\t\/\/ The intro is the first plain &lt;p&gt; without a form field, whatever language or wording WordPress uses.\n\treturn (string) preg_replace_callback(\n\t\t'#&lt;p&gt;((?:(?!&lt;\/p&gt;).)*)&lt;\/p&gt;\\s*#s',\n\t\tfunction ( $m ) use ( $html ) {\n\t\t\tif ( false !== stripos( $m&#91;1&#93;, '&lt;input' ) ) {\n\t\t\t\treturn $m&#91;0&#93;;\n\t\t\t}\n\t\t\treturn '' === $html ? '' : '&lt;p class=\"dpc-password-message\"&gt;' . $html . '&lt;\/p&gt;';\n\t\t},\n\t\t$output,\n\t\t1\n\t);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>WordPress builds the form in <code>get_the_password_form()<\/code> and passes the HTML through the <code>the_password_form<\/code> filter. The intro text is the first plain <code>&lt;p&gt;<\/code> in that form; the next one holds the field and the button.<\/li>\n\n\n<li>The snippet replaces the first <code>&lt;p&gt;<\/code> that has no <code>&lt;input&gt;<\/code> in it with your message. The field, the button, the hidden <code>redirect_to<\/code> and the invalid password notice stay as WordPress made them.<\/li>\n\n\n<li>Searching for the English text breaks on translated sites, and it also broke when WordPress reworded the sentence (&#8220;password-protected&#8221;, with a hyphen). Matching the paragraph avoids both.<\/li>\n\n\n<li>The message goes through <code>esc_html()<\/code>, the URL through <code>esc_url()<\/code>. A URL that <code>esc_url()<\/code> rejects, such as <code>javascript:<\/code>, gives no link at all.<\/li>\n\n\n<li>There are no inline styles. The paragraph has the class <code>dpc-password-message<\/code> and the link <code>dpc-password-request<\/code>.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Everywhere<\/strong>, tick <strong>PHP<\/strong> under <strong>Script location<\/strong>, paste the code into the PHP editor, edit the config and publish. Leave <strong>Action\/Hook<\/strong> empty. The password form can appear in templates, blocks and page builders, and Everywhere covers them all.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The same code also works unchanged in a small plugin or an mu-plugin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Your text: change <code>message<\/code>. Set it to <code>''<\/code> with an empty <code>link_url<\/code> to remove the paragraph and show only the field.<\/li>\n\n\n<li>Request link: set <code>link_url<\/code>, for example <code>home_url( '\/request-password\/' )<\/code> for a page on your site or <code>'mailto:office@example.com'<\/code>, and change <code>link_text<\/code>.<\/li>\n\n\n<li>Other markup, or a different message per post: filter <code>dpc_password_message_html<\/code>. It gets the HTML and the post.<\/li>\n\n\n<li>Style it in your theme&#8217;s stylesheet, for example:<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"css\" class=\"language-css\">.dpc-password-message {\n\tmax-width: 40rem;\n}\n\n.dpc-password-request {\n\tfont-weight: 600;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A theme or plugin that builds its own password form without a plain <code>&lt;p&gt;<\/code> intro is left untouched.<\/li>\n\n\n<li>The form posts to <code>wp-login.php?action=postpass<\/code>. If you restrict <code>wp-login.php<\/code>, keep that action open; <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/lockdown-admin-backend-to-your-ip\/\">Restrict WordPress Admin and Login to Your IP Address<\/a> does.<\/li>\n\n\n<li>Password protection hides the content, not the attachments. Uploaded files stay reachable by their URL.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The older version of this post replaced the English sentence with <code>str_replace()<\/code>, linked to a hard-coded <code>\/request-password\/<\/code> URL with inline styles, and did nothing on translated sites or on WordPress versions with the new wording.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Change or remove the WordPress password protected message, add a link to request the password, and keep it working on translated sites.<\/p>\n","protected":false},"author":5,"featured_media":46691,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Change the WordPress Password Protected Message","description":"Change or remove the WordPress password protected message, add a link to request the password, and keep it working on translated sites."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[76],"dpc_library":[204],"class_list":["post-45717","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-password"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=45717"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45717\/revisions"}],"predecessor-version":[{"id":46690,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45717\/revisions\/46690"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46691"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=45717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=45717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=45717"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=45717"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}