Wordpress category.php not working in /?cat - php

After updating (or maybe not) but some time before, when I'm going to category list page mysite.com/?cat=10 I'm getting not category.php template - it opens single.php Why?
I was trying to make category-10.php (just for test) but still coming to single.php.
Where I should look about my problem?
The problem was with my ajax plugin his code is :
<?php
/**
* Plugin Name: Ajax content loader
* Description: Load content througt ajax.
* Version: 0.1
*
*/
/**
* Initialization. Add our script if needed on this page.
*/
function ajax_content_init() {
global $wp_query;
// Add code to index pages.
if( !is_singular() ) {
// Queue JS
wp_enqueue_script(
'load-posts',
plugin_dir_url( __FILE__ ) . 'js/load-posts.js',
array('jquery'),
'1.0',
true
);
$wp_query->is_single = true;
}
}
add_action('template_redirect', 'ajax_content_init');
?>
Any ideas what is wrong here?
It is for mobile version, how to activate him only for mobile? not for desktop?

Download the WordPress Debug Bar Plugin. It will add a button to your wp-admin bar that says "Debug". When pressed, this button will show you the debug panel. Find the tab in this debug panel that says "WP Query", and it should show you query vars and the template file that are being used.
This should help you figure out what query vars are being registered, and which template file is really loaded.

You should read wordpress codex for template_redirect before trying to experiment things.
This action hook executes just before WordPress determines which
template page to load.
What are you actually trying is:
if( !is_singular() ) {
If page is not a singular, means is not a template page.php or single.php it should execute the code.
So when you were at category.php it executed the code and sets the global $wp_query layer objects property single to true. So whatever template you were viewing at that time it will set that page as single.
Hope it helps.

Related

Creating a shortcode from a backend page

I am trying to create a shortcode from a page that currently resides in the back end. The page has several acf fields as part of a form that creates a request. I would now like to have the same page on the front end. I have tried following the syntax of creating a shortcode from a function after reading about shortocdes, its api and doc and several different tuts online.
add_shortcode('create_requests', array($this, 'load_custom_wp_admin_style'));
^ The attempt above didn't work and I don't get any output when I include the shortcode in a new page.
You can notice that the function I am trying to use 'load_custom_wp_admin_style' returns a null value and uses hooks.
This is the file that contains the function.
Try to include file like below code. I checked your file according to me you need use the plugin url it seems like you are developing the plugin
wp_register_style('your_namespace', plugins_url('style.css',__FILE__ ));
wp_enqueue_style('your_namespace');
wp_register_script( 'your_namespace', plugins_url('your_script.js',__FILE__ ));
wp_enqueue_script('your_namespace');
Assuming that the page you want to display on the front end is a normal WordPress page - created in the pages tab, post type page.
Very simply you can just use the following PHP code to include it in a template:
<?php
$page = get_post(192994);
echo $page->post_content;
?>
If it needs to be a shortcode you can add this into your functions.php:
function output_page_function($atts) {
$page_id = $atts['page_id'];
if (!$page_id) return false;
$page = get_post($page_id);
return $page->post_content;
}
add_shortcode('output_page', 'output_page_function');
And include a shortcode where desired (with 'page_id' attribute)
[output_page page_id=192994]
If it's not a WordPress page, but an actual wp-admin screen, then this would be significantly more difficult/not possible.

Setting Wordpress front page as Algolia search

I've recently setup a new Wordpress install to act as a survey database. The purpose of the site is to collect survey data and allow the admin's to filter and search submitted survey data.
I've installed and configured the Algolia search WP plugin. Everything is working properly. If I navigate to 'mydomain.com/?s=' I see the search form and it's returning results.
My question is how can I set the Algolia search page as my Wordpress index/front page? Or, how can I import this form to a page that I can designate as my WP static front page?
Further info: I have a child theme installed and can create custom page templates/template-parts
The reason is this code here...
if ( is_search() && $settings->should_override_search_with_instantsearch() ) {
return $this->load_instantsearch_template();
}
from this file
https://github.com/algolia/algoliasearch-wordpress/blob/1a51d3e2e8be12bfcbccd1ef2a722911a99376e7/includes/class-algolia-template-loader.php
Essentially it isn't being loaded at present where you want.
Putting this code in your functions.php will fix it.
add_action( 'wp_enqueue_scripts', function () {
// Enqueue the instantsearch.js default styles.
wp_enqueue_style( 'algolia-instantsearch' );
// Ensure jQuery is loaded.
wp_enqueue_script( 'jquery' );
// Enqueue the instantsearch.js library.
wp_enqueue_script( 'algolia-instantsearch' );
// WordPress utility useful for using underscore templating.
wp_enqueue_script( 'wp-util' );
// Allow users to easily enqueue custom styles and scripts.
do_action( 'algolia_instantsearch_scripts' );
} );
Then just add the instantsearch.php code or include the file to the index page/page you want to load it on.
(I just replaced the index.php code with the code from instantsearch.php and it worked just fine)
Hope that helps.

How to hide plugins style sheets in wordpress

i want to hide few plugins style sheets to reduce load on our Index page and categories pages. Actually we want to display plugin style sheet only on Post not on other pages.
we have used following code in plugin, but it doesn't work. please help how to use it.
if( is_single() || is_singular('post') ) wp_enqueue_style('savrix-style.css');
If you are modifying your own plugin I see no reason your code wouldn't work. The is_single() condition is not needed, and will result in the stylesheet being loaded on custom post types and other singles that you don't intend.
However your wp_enqueue_style call is incomplete, so unless you have a wp_register_style call somewhere else defining the handle and URL of the stylesheet you need to change it to something along these lines:
if (is_singular('post')) {
wp_enqueue_style('savrix-style', plugins_url('savrix-style.css', __FILE__);
}
However, I get the impression that you are actually trying to remove a stylesheet included by a thirdparty plugin. It is generally a bad idea to modify a third-party plugin, as your modifications will be lost on the next update... it is very difficult to maintain that sort of modifications in the long run.
Instead make a new plugin and modify whatever you need from there.
What you want to achieve can be accomplished by:
Create a new folder in the wp-content/plugins folder, fx. my_load_reducer.
Inside that folder create a new file called my_load_reducer.php
Paste this in the file:
<?php
/*
Plugin Name: My Load Reducer
Description: Removes unneeded and unwanted stylesheets from other plugins
Version: 0.1
*/
//Use a class to avoid conflicts
class my_load_reducer {
function __construct() {
//Hook into wp_enqueue_scripts with a high priority
add_action( 'wp_enqueue_scripts', array($this, 'deregister_styles'), 1000 );
}
function deregister_styles() {
//Check that current post is not a single post
if (!is_singular('post')) {
//deregister the stylesheet - this removes the twentyfifteen
//main stylesheet - obviously you need to substitute the handle
//of the stylesheet you actually want to remove
wp_deregister_style( 'twentyfifteen-style' );
}
}
}
//Instantiate the class
$my_load_reducer = new my_load_reducer();
Activate the plugin through the wordpress admin.
You can remove perticular plugin css on selected page.
below code is remove plugin css to other pages and display only on post pages:
/*disable loading plugin css to page and load on post page*/
add_action('wp_print_styles', 'my_deregister_styles', 99999);
function my_deregister_styles()
{
if(!is_single())
{
wp_dequeue_style('plugin-css-handle');
wp_deregister_style('plugin-css-handle');
}
}
where 'plugin-css-handle' is perticular plugin's css handle which you want to remove.

WordPress : How to add custom template in genesis framework

I am creating a custom members area for my client's employees. Basically, what I've done so far is I created a new role=consultants and I gave that role a read only access.
Then I uploaded the Peter's Login Redirect Plugin so that the consultants (employees) land in a page called CONSULTANTS PORTAL. From there, they will be able to access their individual page which it will load as long as the name of the page matches the username given to them. That way they can only see their own page.
To see this process, you can visit this link in the wordpress.org forums EASY CLIENT PORTAL
So I've managed a lot of it, except...I am supposed to duplicate the page.php and then add the script that will make the individual page show up. But, the Genesis Framework is pretty complicated. The page.php has an empty skeleton and the actual meat of the page is in a li/structure root folder (That's what I think anyway) .
As it is right now, I have the following in my default template page consultants-portal.php
<?php
/**
* Template Name: Consultants Portal
*/
global $current_user;
get_currentuserinfo();
$page = get_page_by_title($current_user->user_login);
_e($page->post_content);
genesis();
?>
This code gets me this. You can see the content (my page) loading before the page loads. Which tells me there is something else I need to add to this so that the content loads in the actual white area of the page.
The instructions in the link I mentioned says to add the dynamic script right above the is_page or have_posts, but I as I said, Genesis doesn't have this in page.php. instead it is all broken in pieces and spread through the root.
Sorry if I made this too long to read, I wanted you to have all the info I have.
has anyone done this before?
Try out the following code:
<?php
/**
* Template Name: Consultants Portal
*/
// remove Genesis default loop
remove_action( ‘genesis_loop’, ‘genesis_do_loop’ );
// add a custom loop
add_action( ‘genesis_loop’, ‘my_custom_loop’ );
function my_custom_loop () {
// add your queries or custom structure here
global $current_user;
get_currentuserinfo();
$page = get_page_by_title($current_user->user_login);
_e($page->post_content);
}
genesis(); ?>
Instead of writing the code directly, write it inside the loop function as above.

Enqueue Different Stylesheet in wordpress depending on user input via meta box

I am creating a wordpress plugin from which user can select different styles from the plugin options panel. I want to enqueue css file via wp_enqueue_style() according to the user input.
I am getting error when I want to get the metabox value inside the plugins php file.
Error Says:
Calling undefined variable $... on line no ..."
Is there any way I can get the value of the metabox inside php file and enqueue the appropiate css file according to the user input on dashboard.
I am using Custom-Metaboxes-and-Fields-for-WordPress plugin to retrieve user input from plugin option panel for this purpose
My code:
function easyloader_theme() {
global $post;
$theme = easyloader_get_option( 'easyloader_theme' );
wp_register_style('easyloader_style', plugins_url('themes/pace-theme-'. $theme .'.css',__FILE__),'','1.0.0.', false);
wp_enqueue_style('easyloader_style');
}
add_action( 'init', 'easyloader_theme');
I can easily get value of this user input in any when I paste this code in themes php file like (header.php, page.php, footer.php ) via
<?php global $post; $theme = easyloader_get_option( 'easyloader_theme' ); echo $theme; ?>
I just want to get the value inside my plugin php file not in the theme file.
The proper way to enqueue is with the following action:
add_action( 'wp_enqueue_scripts', 'easyloader_theme' );
function easyloader_theme() {
/* Enqueue Scripts AND Styles */
}
Looks like the plugin has some internal cache, but at the end you can try using WP function directly:
get_option( 'easyloader_theme' );

Categories