I want to add a special hiding tag to hide posts-pages-products text after it for unregistered users and show a phrase like “To read a full article text - please, Register”. For a such purpose I create a micro plugin. But it does not work.
function hide_replace_register( $content ) {
if (!is_user_logged_in()) {
$updated_content = explode("<!--hide-->", $content);
$updated_content = explode("<!--hide-->", $content);
$updated_content = explode("<!–hide–>", $content);
$ucontent = $updated_content[0];
if ($ucontent !== $content) {
$ucontent .= '<a href="' . wp_registration_url() .'"><strong>To read a full article text - please, Register!</strong></a>';
} else {
$ucontent = $content;
}
return $ucontent;
}
}
add_filter('the_content', 'hide_replace_register', 10);
Can you please help with bug correction?
I want to add a special hiding tag to hide posts-pages-products text after it for unregistered users and show a phrase like “To read a full article text - please, Register”. For a such purpose I create a micro plugin. But it does not work.
function hide_replace_register( $content ) {
if (!is_user_logged_in()) {
$updated_content = explode("<!--hide-->", $content);
$updated_content = explode("<!--hide-->", $content);
$updated_content = explode("<!–hide–>", $content);
$ucontent = $updated_content[0];
if ($ucontent !== $content) {
$ucontent .= '<a href="' . wp_registration_url() .'"><strong>To read a full article text - please, Register!</strong></a>';
} else {
$ucontent = $content;
}
return $ucontent;
}
}
add_filter('the_content', 'hide_replace_register', 10);
Can you please help with bug correction?
Share Improve this question asked Aug 21, 2019 at 21:12 justdudejustdude 215 bronze badges1 Answer
Reset to default 0The first thing I see wrong is your first "If" statement. It wrapps everything, including your "return" statement.
Assuming the rest works, you might try this:
function hide_replace_register( $content ) {
if (!is_user_logged_in()) {
$updated_content = explode("<!--hide-->", $content);
$updated_content = explode("<!--hide-->", $content);
$updated_content = explode("<!–hide–>", $content);
$ucontent = $updated_content[0];
if ($ucontent !== $content) {
$ucontent .= '<a href="' . wp_registration_url() .'"><strong>To read a full article text - please, Register!</strong></a>';
} else {
$ucontent = $content;
} // end if content has been modified
} else { // else .. user IS logged in, so return the original content
$ucontent = $content;
} // end if user is logged in
return $ucontent;
} // end function hide_replace_register
add_filter('the_content', 'hide_replace_register', 10);
I am a bit suspicious of multiple explodes in a row, without any array_merge function, but this modification will at least output something for logged in users.