PHP include for just homepage in wordpress (genesis framework) - wordpress

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');
}

Related

Replacing part of header through functions.php

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() ) ) {

Relocate wordpress plugin output thats hooked into the content

I am trying to relocate the position of the output generated by a review plugin for wordpress. The plugin hooks into the end of "the_content". I have found the filter which works to remove the output:
remove_filter('the_content', array( EDD_Reviews::get_instance(), 'load_frontend'));
Now I am going crazy trying to relocate the output to a new location. I have created a custom hook in my theme file:
function reviews() {
do_action('reviews');
}
The above hook outputs just fine to custom location in the theme files when passed a simple function. However the tricky part is getting the reviews and review form to display. I tried adding it back with this:
add_action('reviews', array( EDD_Reviews::get_instance(), 'load_frontend'));
This did not work. The full function I am trying to call is below. Any body have any ideas how I may call this via my new hook? I am rather stuck.
public function load_frontend( $content ) {
global $post;
if ( $post && $post->post_type == 'download' && is_singular( 'download' ) && is_main_query() && ! post_password_required() ) {
ob_start();
edd_get_template_part( 'reviews' );
if ( get_option( 'thread_comments' ) ) {
edd_get_template_part( 'reviews-reply' );
}
$content .= ob_get_contents();
ob_end_clean();
}
return $content;
}
Many thanks in advance.
Ok mostly figured this out after studying classes. If anyone else is looking to do this then remove the hook as below:
remove_filter('the_content', array( EDD_Reviews::get_instance(), 'load_frontend'));
The following line will then render the reviews and form anywhere you like:
<?php echo EDD_Reviews()->load_frontend( '' ); ?>

Open WooCommerce External Products in New Tab

I'm trying to customize WooCommerce external product links to open in new tabs...
This is my try:
added a filter to the WordPress theme functions.php file as the following:
add_filter( 'woocommerce_product_add_to_cart_url', 'woocommerce_externalProducts_openInNewTab' );
function woocommerce_externalProducts_openInNewTab($product_url) {
global $product;
if ( $product->is_type('external') ) {
$product_url = $product->get_product_url() . '"target="_blank""';
}
return $product_url;
}
What did I missed?
what you're currently doing is wrong... get_product_url is named as what it do. It will give you the url... not the html anchor that has the url, but just the url.. so you're just adding some text to the url.. that's what you are doing...
One solution is given by #Ash Patel. You can change the markup by using templates... just navigate to your plugin folder and look for this file.. woocommerce\templates\single-product\add-to-cart\external.php. You can find instructions inside it.
Now, sometimes, we don't like editing templates... especially if it's just minor edits like this...
Below code will do it the way you want it... just paste this code in your theme's functions.php.
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
add_action( 'woocommerce_external_add_to_cart', 'rei_external_add_to_cart', 30 );
function rei_external_add_to_cart(){
global $product;
if ( ! $product->add_to_cart_url() ) {
return;
}
$product_url = $product->add_to_cart_url();
$button_text = $product->single_add_to_cart_text();
do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<p class="cart">
<?php echo esc_html( $button_text ); ?>
</p>
<?php do_action( 'woocommerce_after_add_to_cart_button' );
}
Here is how you add target="_blank" to the links on archive pages:
function ns_open_in_new_tab($args, $product)
{
if( $product->is_type('external') ) {
// Inject target="_blank" into the attributes array
$args['attributes']['target'] = '_blank';
}
return $args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'ns_open_in_new_tab', 10, 2 );
Replace ns_ part with your own namespace abbreviation.
Remove above funtion from function.php:
Use plugin files from Template folder by Template Overwrite method and then
follow below path:
woocommerce\templates\single-product\add-to-cart\external.php
open external.php where there is a tag, apply target="_blank".
it will work.

WordPress Function on first Theme Activation

is there a way to execute a function only on the first activation of a specific theme?
I found this code but the problem is that the function will execute after every theme switch
<?php add_action("after_switch_theme", "mytheme_do_something"); ?>
Yes you can by writing and checking an option.
add_action("after_switch_theme", "mytheme_do_something");
function mytheme_do_something() {
if( ! get_option( 'mytheme_setting' ) ) {
add_option( 'mytheme_setting', '1');
// some things you want to do
}
}

Virtual Page Within Theme Template

I am creating a plugin that needs a virtual page in order to dynamically create a "Review Order" page (Orders are a custom post type I created). I was able to create a virtual page using the following code:
// Set up the rewrite rules for the order page
add_action( 'init', 'action_init_redirect' );
function action_init_redirect() {
add_rewrite_rule( 'orders/?', 'index.php?orders=new', 'top' );
}
add_filter( 'query_vars', 'filter_query_vars' );
function filter_query_vars( $query_vars ) {
$query_vars[] = 'orders';
return $query_vars;
}
add_action( 'parse_request', 'action_parse_request');
function action_parse_request( &$wp ) {
if ( array_key_exists( 'orders', $wp->query_vars ) ) {
//Beginning of page code
echo "hello";
exit;
}
}
The problem is that this creates a page with a blank template, that is, the above code creates a blank page with the text hello. I would like for the virtual page to be within the theme of the site and displayed like a regular page within the WordPress framework. How to accomplish this?
A solution is to add a template page inside parse_request:
function action_parse_request( $wp ) {
if ( array_key_exists( 'virtual', $wp->query_vars ) ) {
get_header(); ?>
<div id="primary">
<div id="content" role="main">
Hello, world!
</div>
</div>
<?php get_footer();
exit;
}
}
try using the following:
define('WP_USE_THEMES', true);
require('/ABSOLUTE_PATH_HERE/wp-blog-header.php');
require('/ABSOLUTE_PATH_HERE/wp-load.php');
you will need to add this to the top of your page.

Resources