WordPress snippet - post password protection check - wordpress

Hello i want to run a content modification snippet but only on password protected posts.
function func($content) {
if( $post->password_protected ){
$content = [whatever]
}
return $content;
}
add_filter('the_content', 'func');
how can i do that password_protected check?

I figured it out
if( !empty($GLOBALS['post']->post_password) ){..}

Related

WordPress quick access link for protected page

I'm trying to create quick access link for protected page. For example if the person went to http://example.com/post/?password=PASSWORD they would be straight in without being asked for the password.
So far I've added filter for the 'the_content' which checks if the post is protected, and it compares the value from url with post password.
This part works fine, but how do I return the content after that? Is there any way I can simulate the password form submission, or get content somehow?
function render_content_or_not_to_render( $content ) {
global $post;
if ( post_password_required() ) {
if ( $_GET['password'] == $post->post_password ) {
return $content; // this way it just returns the password form
} else {
return get_the_password_form();
}
}
}
add_filter( 'the_content', 'render_content_or_not_to_render' );
Added filter for the 'post_password_required' gets the job done. Not sure about security, please consider this as rookie solution.
function modof_post_password_required( $post = null ){
global $post;
if ( $_GET['password'] == $post->post_password ) {
return false;
} else {
return true;
}
}
add_filter( 'post_password_required', 'modof_post_password_required', 0 );

Search Widget in Blog page in wordpress

I have added search widget on my Blog page. when I click on blank search it filters out all the page of my website. I want to add setting using coding that it only filters out the blog post.
I have added this code for that
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
}
}
add_action('pre_get_posts','search_filter');
but using this code, by search product functionality stop working.
Goto wp-includes/query.php
Find below and
$q[‘post_type’] = ‘any’;
replace
$q[‘post_type’] = ‘post’;
or better way to put this code in functions.php file to overcome the issue after any wordpress update
if (!is_admin()) {
function wpb_search_filter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','wpb_search_filter');
}

Wordpress Plugin Custom Post Type Archive Template results in other pages not displaying

This is the code I am using to call my archive template, now the knowledgebase section works, the forums work, but all other pages throughout the website are now blank.
function kb_archive_template_function($arhive_template){
if(is_post_type_archive('knowledgebase')){
$theme_files = array('/templates/archive-knowledgebase.php');
$exists_in_theme = locate_template($theme_files, false);
if($exists_in_theme == ''){
return plugin_dir_path(__FILE__) . '/templates/archive-knowledgebase.php';
}
return $archive_template;
}
}
You can use this code ,instead of your code
function get_custom_post_type_template( $archive_template ) {
global $post;
if ( is_post_type_archive ( 'knowledgebase' ) ) {
$archive_template = plugin_dir_path(__FILE__) . '/templates/archive-knowledgebase.php';
}
return $archive_template;
}
add_filter( 'archive_template', 'get_custom_post_type_template' ) ;
Try that, then let me know the result. Thanks

Elementor page builder shortcode issue

I'm facing a weird problem when using the Elementor Wordpress Page Builder.
After creating a custom shortcode and inserting it into any page position, it also shows up at the top of the page, but only in Edit mode.
Top of the page:
Place where I want to insert shortcode:
This answer on an unrelated site helped me solve this Elementor issue. https://wp-types.com/forums/topic/shortcode-output-showing-up-in-the-wrong-place/
I just had to include ob_start(); and $content = ob_get_clean(); return $content; in my function. Here is what it looks like:
function custom_author_link_function() {
ob_start();
coauthors_posts_links();
$content = ob_get_clean();
return $content;
}
add_shortcode('custom_author_link', 'custom_author_link_function');
This is my working example:
function name_it( ){
ob_start();
function_name();
$content = ob_get_clean();
return $content;
return function_name();
}
add_shortcode( 'shortcode_name', 'name_it' );
Just look at function_name(); and return function_name(); lines to avoid errors.

wordpress shortcode problem

Apparently the following simple code breaks the shortcode API of wordpress. When I add this code in the function.php the shortcode API won't work.
This code is normally to add a text at the bottom of each page, any idea why?
function cmstut_basic_promote($content)
{
echo $content;
if(is_single())
{
?>
<div class="promote">
<h2>Enjoy this article?</h2>
<p>If you have enjoyed this article please subscribe to our RSS Feed</p>
</div>
<?php
}
}
add_filter('the_content', 'cmstut_basic_promote');
you must return the content from your filter not echo it - so something like
function cmstut_basic_promote($content) {
if(is_single()) {
return $content . '<div class="promote"><h2>Enjoy this article?</h2> ...';
} else {
return $content;
}
}
would be the way to go

Resources