WP custom comment field only on certain page - php

I added an additional field to my comment section of my wordpress page with the help of a little plugin, based on a tutorial https://www.smashingmagazine.com/2012/05/adding-custom-fields-in-wordpress-comment-form/.
It's only one field I need (ZIP Code).
The plugin works fine, but it adds the custom field on every page. I just want it on one specific page.
I tried to wrap all the plugin code into one function that loads on the specific page:
add_action('template_redirect', 'load-on-certain-page');
function load-on-certain-page(){
if ( is_page(23) ) {
//Complete Plugin Code
}
}
Basically that works, but the check for the empty field isn't working anymore.
add_filter( 'preprocess_comment', 'verify_comment_meta_data' );
function verify_comment_meta_data( $commentdata ) {
if (empty( $_POST['title'] ) )
wp_die( __( 'Fehler: Bitte geben Sie Ihre Postleitzahl ein.' ) );
return $commentdata;
}
If I exclude the field check from my load-on-certain-page() function it checks the field everywhere so thats no solution. I also tried to add an additional condition to the if-statement (is_page()), but that does not work, too.
Can you point me into the right direction how to make my plugin functions work only on that certain page? And what "best practise" is to do it?
Thanks!

You can can possibly add an if/else statement to only show the field on that specific page...
if ( $post->ID === 23 ) {
// Input for zip code on page 23
} else {
// No input for zip code
}

Finally I got it.
The problem is, that is_page() etc. didn't work for the "preprocess_comment" filter.
But you can check the page ID the comment will be assigned to by using:
$post_id = $commentdata['comment_post_ID'];

Related

Load custom page templates in WordPress admin dashboard

I have several custom page templates in my theme. But I want to hide a few with a plugin, and only show the custom home page template on the page that has been set as the front page "is_front_page" or "is_home". This is what I am using in my plugin to stop some of the page templates from showing up.
This is for a large multisite where there are two tiers of sites, one gets the full set of features, and the other a stunted set. I have everything working as I need except for this.
add_filter( 'theme_page_templates', 'je_remove_page_template' );
function je_remove_page_template( $pages_templates ) {
unset( $pages_templates['page-topimage.php'] );
unset( $pages_templates['page-home.php'] );
return $pages_templates;
}
The code above works totally fine, but I need a conditional in there to show the page-home.php when the page has been set to the home page. I have tried this code, but it doesn't work.
if ( is_front_page() ) :
add_filter( 'theme_page_templates', 'je_remove_page_template' );
function je_remove_page_template( $pages_templates ) {
unset( $pages_templates['page-topimage.php'] );
return $pages_templates;
}
else :
function je_remove_page_template( $pages_templates ) {
unset( $pages_templates['page-topimage.php'] );
unset( $pages_templates['page-home.php'] );
return $pages_templates;
}
endif;
Any ideas on how I can get this to work?
A few things:
One, it appears you've only added the add_filter call to the is_front_page()'s true condition, that's probably part of it.
Second, you should not be defining and/or redefining functions inside if statements!
Third, for WordPress specifically, with it's Action Hook and Filter system, it's generally considered a best practice to add the filters and actions at all times, and run if/else or abort type statements inside the function to allow for easier extensibility.
Edit:
Based on some clarification, I understand a bit better. You need to get the ID of the page that's set to the front page, which is stored as an option named page_on_front in the options table. The is_front_page() function will only work in the scope of the current $wp_query loop.
I think this would solve it for you. Run the je_remove_page_template function on the theme_page_templates filter, and check the current page's ID against the one stored in the page_on_front option:
add_filter( 'theme_page_templates', 'je_remove_page_template' );
function je_remove_page_template( $pages_templates ){
unset( $pages_templates['page-topimage.php'] );
$home_page_id = absint( get_option( 'page_on_front' ) );
$this_page_id = get_the_ID();
// If this isn't the home page, remove `page-home`
if( $this_page_id != $home_page_id ){
unset( $pages_templates['page-home.php'] );
}
return $pages_templates;
}
Also of note would be that is_front_page() is different than is_home() - make sure you're using the correct one! (I believe you are, as generally IFP is what people want)

Adding button to the opposite side of entry-title

Im trying to add a button to the opposite side of entry-title specific page of wordpress customizr theme.
Here's my ref. https://presscustomizr.com/customizr/hooks-api/ theme page but I really dont know how to do.
I tried a php functions but it doesnt work.
Need some help please.
add_action ('__before_archive_title', 'button_beside_title');
function button_beside_title() {
if ( !is_page( array( '3154' ) ) ){
echo 'Text Example';
}}

Cannot search products on wp-admin of woocommerce

Scenario: I have my searchbox on my front page of my website. On that searchbox, I can search the product by SKU and product name.
Problem: I can search a product on client's page/Front-end but not on wp-admin of the website.
I already tried this : Woocommerce cannot see products in wp-admin , but still no result found.
Something strange!
Does anybody know?
For the sake of those developers who may also encounter same problem, that's why I want to put it here for future reference.
I have been backtracking and searching answers on the internet but I found none. I tried to review my functions.php inside the theme and found this code below:
function __search_by_title_only( $search, &$wp_query ) {
global $wpdb;
$entry = isset($_GET['s']) ? $_GET['s'] : '';
if( $entry ){
$search = "MY SQL QUERY HERE.....";
}
return $search;
}
add_filter( 'posts_search', '__search_by_title_only', 500, 2 );
The above code was inserted inside the functions.php in order to overwrite the query.
posts_search filter, overrrides the product search on my back-end of search though my query was mistaken that's why the problem exists.
I just removed the code and everything works fine.

wordpress functions.php - use different page template for each post category

I want to hook into the save_post function, find out what category the post is in, and then assign a different page template for posts in each category. I've tried about 30 different versions of this with no luck. Will someone please help point me in the right direction?
add_action( 'save_post', 'assign_custom_template' );
function assign_custom_template($post_id) {
$category = get_the_category($post_id);
$cat_id = $category->cat_ID;
if( $cat_id == 1 ) {
update_post_meta($post_id, "_wp_page_template", "template1.php");
}
if( $cat_id == 2 ) {
update_post_meta($post_id, "_wp_page_template", "template2.php");
}
}
You just need to create category-1.php which rendered as template1.php and category-2.php which rendered as template2.php in your theme root.
See template hierarchy for more info.
I tried to emulate the official WP hierarchy scheme among my posts & custom post types, but it just wasn't happening. I ended up using Custom Post Types so that I could assign templates to both the "list" pages and the "individual" pages. And then I wrote some javascript that looks for the post-type string in the URL, and if it's detected, it adds the current_page_parent/ancestor classes to the appropriate menu items. Not perfect or totally future-proof, but it gets the job done.
If someone comes up with a better solution, please post it!

Wordpress URL Routing, Multiple permalinks with different templates

This question is based on an unanswered question in Wordpress Development which has not gotten a solid answer.
I have a wordpress website which lists hotels. the url for a single hotel looks like:
/hotels/the-marriot-hotel
I also have a custom taxonomy for Locations, which allows me to browse the hotels in various locations, which works fine, the urls are like:
/Locations/Liverpool
For URL's like /hotels/* I would like to use a custom template, which I have done already and works fine.
The Problem
I also want to be able to drilldown the Locations taxonomy creating a breadcrumb type URL and also use a different template for the hotel page.
For Example, if a user is browsing /Locations/Liverpool and clicks the Marriot Hotel I would like it to click through to /Locations/Liverpool/the-marriot-hotel instead of /hotels/the-marriot-hotel and also use a slightly different template, which can also load a different sidebar and recommend other hotels in the area specific to the location slug in the URL
So basically I want two routes to a single post and a different template used based on the route used.
How would I go about implementing this?
What have I tried?
I've tried adding a new page and using a rewrite rule to point to it to be the locations hotel page.
I've tried adding a slug on the end of the /Locations/{location-slug} url and reading this in the page template and loading the hotel post instead of the list it doesn't seem to be working but also feels like a terrible hack anyway
An idea that I've had is to add a rewrite to the hotels/{slug} page and using code to detect the URL used and switch templates dynamically but I'm not sure this is the best approach
I have managed to get this working using the second method mentioned above (adding a rewrite to the locations landing page and checking for a query_var).
I will post the code below that I used but although this works and seems to be working very well, It does not feel like the best way of doing it. If someone know of a better way of doing this please post the answer.
I used this online post for reference.
Note: The listing page shows the list of hotels in the taxonomy in a side column down the side and shows the currently selected or a random one in the main content area. Which will explain how I am using the loop below.
function prefix_locations_rewrite_rule() {
add_rewrite_rule( 'Locations/([^/]+)/([^/]+)', 'index.php?locations=$matches[1]&hotel=$matches[2]', 'top' );
}
function prefix_register_query_var( $vars ) {
$vars[] = 'hotel';
return $vars;
}
function prefix_url_rewrite_templates() {
if ( get_query_var( 'hotel' ) && is_singular( 'hotel' ) ) {
add_filter( 'template_include', function() {
return get_template_directory() . '/taxonomy-locations.php';
});
}
}
add_action( 'template_redirect', 'prefix_url_rewrite_templates' );
add_filter( 'query_vars', 'prefix_register_query_var' );
add_action( 'init', 'prefix_locations_rewrite_rule' );
In my template file for the hotels landing page:
$hotelSlug = get_query_var( 'hotel', false);
if ( have_posts() ) {
while (have_posts()) : the_post();
if ($post->post_name == $hotelSlug) {
break;
}
endwhile;
}
This bit of code will iterate over the posts and if the hotel slug matches the query var it will break there so that the current post is the one we wanted.
We could just use a query here but as I already have a list of posts within the taxonomy I thought I'd just iterate over it. Below this I check to see if a specific hotel has been selected otherwise I show a random one from the list.
I am still to add additional logic and error handling to this code, I hope it helps someone with a similar issue

Categories