In the non profit that I'm a part of, we teach people how to sew and make handcrafted patterns and what not. That said, some of the work needs to be protected without the option of a login.
I stumbled across some code online that got me thinking of how we might be able to do this, but I am not very good at understanding how everything inside WordPress works even though I do my best in reading the codex and what not.
Anyhow, the idea is to use a category within the the_content filter and that way, control parts of the post content. We have decided to use a highlighter for our design pattern instructions, making it easier for elderly people to read and to make it stand out.
This means we're using <pre> and <code> HTML tags. This is where the hard part for me begins. We only want to protect posts that are assigned to the lesson category; in other words - the <pre> and <code> tags should be protected if the post is assigned to the lesson category.
We are looking for a way to control the <pre> and <code> tags within the content based on category and to be able to only provide the lesson key once as it is, from what I understand, supposed to be set in a cookie.
We are all very grateful for all the help we can get.
This is the original code that I found.
add_filter( 'the_content', 'secured_lesson_instructions' , 9 );
function secured_lesson_instructions( $content ) {
$content = preg_replace('/(<pre[^>]*>\s*<code[^>]*>)/',"[lesson]$1", $content));
$content = preg_replace('/(<\/code>\s*<\/pre>)/', "$1[/lesson]", $content);
return $content;
}
This is my altered version, which makes the content go blank for ALL posts no matter the category.
add_filter( 'the_content', 'secured_lesson_instructions' , 9 );
function secured_lesson_instructions( $content ) {
// this is my added if statement to make sure the protection only runs for lesson posts
if ( in_category('lesson') ) {
$content = preg_replace('/(<pre[^>]*>\s*<code[^>]*>)/',"[lesson]$1", $content));
$content = preg_replace('/(<\/code>\s*<\/pre>)/', "$1[/lesson]", $content);
return $content;
}
}
After doing my best in educating myself, this is my second attempt to make it work - which work and yet, not. This version partially work as it leaves the [/lesson] shortcode printed in text on the post.
add_filter( 'the_content', 'secured_lesson_instructions' , 9 );
function secured_lesson_instructions( $content ) {
if (in_category('lesson') )
$content = preg_replace('/(<pre[^>]*>\s*<code[^>]*>)/',"[lesson]$1", $content);
$content = preg_replace('/(<\/code>\s*<\/pre>)/', "$1[/lesson]", $content);
return $content;
}
This is my third attempt, which works as intended but which renders the [lesson] shortcode cookie ineffective as you have to provide the lesson key each time.
add_filter( 'the_content', 'secured_lesson_instructions' , 9 );
function secured_lesson_instructions( $content ) {
if (in_category('lesson') )
$content = preg_replace('/(<pre[^>]*>\s*<code[^>]*>)/',"[lesson]$1", preg_replace('/(<\/code>\s*<\/pre>)/', "$1[/lesson]", $content));
return $content;
}
This is the lesson shortcode. Please, I need all the help I can get.
add_shortcode( 'lesson', 'secured_lesson_instructions_shortcode');
function secured_lesson_instructions_shortcode( $atts, $content=null ) {
$lessonpass = isset( $_REQUEST['password'] ) ? $_REQUEST['password'] : ( isset( $_COOKIE['lessonpass'] ) ? $_COOKIE['lessonpass'] : NULL );
if ( in_array( $lessonpass, array( '001', '002', '003', '003' ) ) ) {
$return_lesson = do_shortcode( $content );
} else {
$return_lesson = '
<div id="lessonsection" style="margin-top:20px; font-size:15px;">To view this lesson, please submit your lesson key. <a class="get-lesson-key" href="/lesson-key/">Get lesson key here</a>.</div>
<form method="post" onsubmit="lessonCookie( this ); return false;">
<input required style="display: block; width: 69%; height: 50px; margin-right: 1%; float: left; border: 2px solid #333;" type="text" placeholder=" Your Lesson Key Here" name="password" id="lessonpass">
<input style="display: block; margin: 0px; width: 30%; height: 50px; background-color: #333; color: #eee;" type="submit" value="Check Lesson Key">
</form>
<script>
function lessonCookie( form ) {
document.cookie = "lessonpass=" + escape( form.lessonpass.value ) + "; path=/";
</script>
';
}
return $return_lesson;
}
Aucun commentaire:
Enregistrer un commentaire