Replacing part of header through functions.php - wordpress

I am trying to customize parts of the header.php in Wordpress Genesis framework through a code in functions.php like this:
if ( class_exists( 'page-template' ) ) {
remove_action( 'genesis_header', 'genesis_do_header' );
add_action( 'genesis_header', 'gd_genesis_do_header' );
function gd_genesis_do_header() {
echo 'test';
}
}
The goal is to have the modification show up on pages with a certain body class 'page-template'.
The function works without the 'if'-statement' but not with it. What could be wrong here?
Or maybe it is possible to do the action based on a page?

Instead of checking for the page-template class on body, just check if a page template is being used on the page, like this:
if ( is_page_template() ) {
remove_action( 'genesis_header', 'genesis_do_header' );
add_action( 'genesis_header', 'gd_genesis_do_header' );
function gd_genesis_do_header() {
echo 'test';
}
}
Here's more information about WordPress' is_page_template() function. You can even use the function to check if a specific page template is being used. For example, is_page_template( 'about.php' ) would return true if the about.php template was being used.

To check body classes, use get_body_class NOT class_exists which checks for a PHP class used in OOP.
if ( in_array( 'page-template', get_body_class() ) ) {

Related

WordPress dynamically include scripts and styles

I have a basic bunch of CSS/Js files defined in my functions.php file. There I register and enqueue those scripts and stylesheets.
But in specific situations I want to load additional scripts based on the site template which is used.
I tried to register and enqueue those additional scripts in the specific template file, but it didnt work. It does only work when included in the functions.php.
What is the correct way to do this?
You can try like this
add_action( 'wp_enqueue_scripts', 'enqueue_file' );
function enqueue_file() {
if(is_front() || is_archive()){ // you can set here condition
wp_enqueue_style( 'main-css', get_template_directory_uri() .
'/static/css/main.min.css"' );
}
}
add this to your functions.php in child theme:
add_action(
'wp_enqueue_scripts',
'loadActionFrontend'
);
function loadActionFrontend()
{
if( is_page( 'pageid here' ) ) {
wp_register_script(
'scriptName',
'scripturl',
[],
'',
true
);
wp_enqueue_script('scriptName');
}
}
with the query if( is_page( 'pageid here' ) ) you can insert your desired pageid where the script shold loaded.

Use Wordpress Filter to modify value of wp_head output?

I have just started with Worpdress, and see what when creating a custom theme there is an annoying margin at the top of the page, and that is caused by the content output by the wp_head function. Since I need to learn how to use filters, I thought I would use a filter to remove the css for the html and body tags from the wp_head functions.
The question is, how do I do that? Inside the function I use for a filter, how I am able to access the values for the css written out for the html tag in the wp_head function? Have searched by have found no good explanation for this.
You can remove (and add) classes to the <body> tag as long as the theme is using <?php body_class(); ?>
There is a body_class filter hook at the end of get_body_class() in includes/post-template.php
From Codex
apply_filters( 'body_class', array $classes, array $class )
Filters the list of CSS body classes for the current post or page.
Remove a class from the body_class array:
add_filter( 'body_class', function( $classes ) {
if ( isset( $classes['class-to-remove'] ) ) {
unset( $classes['class-to-remove'] );
}
return $classes;
} );
Add specific CSS class by filter:
add_filter( 'body_class', function( $classes ) {
return array_merge( $classes, array( 'class-name' ) );
} );
Styesheets
To remove style sheets, as long as they are registered properly,
function dequeue_some_css() {
wp_dequeue_style('some-css');
wp_deregister_style('some-css');
}
add_action('wp_enqueue_scripts','dequeue_some_css', 100);
Look into wp_dequeue_style and wp_deregister_style
For adding your own style sheets, see wp_enqueue_style and wp_register_style.

PHP include for just homepage in wordpress (genesis framework)

I'm using the Genesis framework and trying to add some bits under the content.
I want them to appear on just the homepage, so I'm currently using this code, however it's including on all pages.
I have a feeling the if statement is wrong...
add_action( 'genesis_after_content', 'sp_homepage_content' );
function sp_homepage_content() {
if ( is_page('2') )
include ('includes/homepage-content.php');
}
EDIT:
I think i fixed it with the following:
add_action( 'genesis_after_content', 'sp_homepage_content' );
function sp_homepage_content() {
?>
<?php if ( is_page('2') ) { include ('includes/homepage-content.php');} else ?>
<?php
}
Although I'm not sure if that's "good" code... It works
Change your condition to if(is_home()).
add_action( 'genesis_after_content', 'sp_homepage_content' );
function sp_homepage_content() {
if ( is_home() )
include ('includes/homepage-content.php');
}

Add custom frontend page without menu item - wordpress

Im trying to do something like this.
Add "custom page" without page
I know about adding a wordpress page from admin panel, Pages->Add New, and then link this page to PHP file using the slug. I've already done that. I just want to make this page work without adding it from admin panel, in case if page gets deleted from admin panel it won't work even if exists in the directory.
Please let me know if my question isn't clear enough. Any help is highly appreciated.
Thanks!
Update:
Thanks to #Mike i was able to solve the problem by modifying his code. I just had to add add_rewrite_rule() and its working good now. Don't forget to flush permalinks.
function add_application_endpoint() {
add_rewrite_endpoint( 'view', EP_PERMALINK );
}
add_action( 'init', 'add_application_endpoint' );
function add_endpoint_queryvar( $query_vars ) {
$query_vars[] = 'view';
$query_vars[] = 'ptag';
$query_vars[] = 'product_cat';
return $query_vars;
}
add_filter( 'query_vars', 'add_endpoint_queryvar' );
add_rewrite_rule( '^view/([^/]+)/([^/]+)/?$', 'index.php?pagename=custom-product-tags&ptag=$matches[1]&product_cat=$matches[2]', 'top' );
/**
* Setting up job app template redirect for custom end point rewrite
*/
function job_application_template_redirect() {
global $wp_query;
if ( $wp_query->query_vars['name'] != 'custom-product-tags' ) {
return;
}
include dirname( __FILE__ ) . '/page-custom-product-tags.php';
exit;
}
add_action( 'template_redirect', 'job_application_template_redirect' );
You can do it by creating a custom endpoint and setting up a template redirect in your functions.php file.. Here is an example for a job application page. With this code added to my functions.php file, if I visit '/apply' on my site, the page-job_application.php template is rendered.
Hope this works for your needs.
/**
* Rewrite custom endpoint for job post applications
*/
function add_application_endpoint() {
add_rewrite_endpoint('apply', EP_PERMALINK);
}
add_action( 'init', 'add_application_endpoint');
/**
* Register our custom endpoint as a query var
*/
function add_endpoint_queryvar( $query_vars ) {
$query_vars[] = 'apply';
return $query_vars;
}
add_filter( 'query_vars', 'add_endpoint_queryvar' );
/**
* Setting up job app template redirect for custom end point rewrite
*/
function job_application_template_redirect() {
global $wp_query;
if ( ! isset( $wp_query->query_vars['apply'] ) || ! is_singular() )
return;
include dirname( __FILE__ ) . '/page-job_application.php';
exit;
}
add_action( 'template_redirect', 'job_application_template_redirect' );

How to add CSS only to the options page of a plugin in WordPress?

I want to add a stylesheet for the options_page of my plugin only. But how to do that? My code so far:
function add_options_page_style() {
wp_register_style('options_page_style', plugins_url('css/options_style.css',__FILE__));
wp_enqueue_style('options_page_style');
}
add_action( 'admin_menu', 'add_options_page_style' );
I could place an if statement before the line with add_action... but I'm not sure how to filter my options page. I already tried the $pagename variable and also this line: $wp_query->queried_object->post_name; but it didn't work.
The filter $_GET['page'] does work but might break in future versions.
Somewhere you'll be registering page like this:-
function register_page(){
global $page_hook_suffix;
$page_hook_suffix = add_options_page('Your_plugin', 'Your_plugin', 'manage_options', __FILE__, 'display_form');
}
add_action('admin_menu', 'register_page');
And while enqueueing script you'll do something like this:-
function my_enqueue($hook) {
global $page_hook_suffix;
if( $hook != $page_hook_suffix )
return;
wp_register_style('options_page_style', plugins_url('css/options_style.css',__FILE__));
wp_enqueue_style('options_page_style');
}
add_action( 'admin_enqueue_scripts', 'my_enqueue' );
function load_custom_wp_admin_style($hook) {
// Load only on ?page=mypluginname
if($hook != 'toplevel_page_mypluginname') {
return;
}
wp_enqueue_style( 'custom_wp_admin_css', plugins_url('admin-style.css', __FILE__) );
}
add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_style' );
add your page slug as suffix to toplevel_page_
e.g. if page slug is this-plugin-options
then
if($hook != 'toplevel_page_this-plugin-options') {
return;
}
here is wordpress doc with
Example: Load CSS File on All Admin Pages,
Example: Load CSS File from a plugin on specific Admin Page

Resources