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.
Related
I am trying to display a custom page using a plugin. It only shows when a URL is matched. The problem is, this page is 404 normally, since no pages or posts exists with that address. So it shows the 404 page title Page not found.
Here is what I am doing
add_filter('template_include', [$this, 'pageTeamplate']);
public function pageTeamplate($templates, $content=''){
global $wp;
if($wp->query_vars['pagename'] != 'forehand-news')
return $templates;
get_header();
wp_enqueue_style('baseplugin-frontend');
wp_enqueue_script('baseplugin-frontend');
$response = wp_remote_request("http://backoffice.localhost/app/news",
array(
'method' => 'GET'
)
);
//var_dump($response);
//echo $response;
$divData = wp_remote_retrieve_body($response);
echo "<script>window.news = $divData </script>";
echo '<div id="vue-frontend-app"></div>';
get_footer();
}
The page shows and its content, but the title remains as Page not found.
How can I change the title or what is the best way to show a custom plugin page against a URL?
Add the following code snippet to your active theme's functions.php file:
function modify_404_page_title( $title_parts ) {
if ( is_404() ) {
$title_parts['title'] = 'ADD 404 TITLE TEXT HERE';
}
return $title_parts;
}
add_filter( 'document_title_parts', 'modify_404_page_title' );
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() ) ) {
I have a website that uses worpdress with catch box theme, and I want to change the 404 page title.
I looked for the "Nothing found for" sentence on the PHP files on the Editor but I did not find nothing.
I searched for the get_header(); method in editor to change the title but I did not find it.
see the title that I want to change
Have been already answered here: https://wordpress.stackexchange.com/questions/30873/how-to-change-404-page-title
Add the following to theme functions.php file
function theme_slug_filter_wp_title( $title ) {
if ( is_404() ) {
$title = 'ADD 404 TITLE TEXT HERE';
}
// You can do other filtering here, or
// just return $title
return $title;
}
// Hook into wp_title filter hook
add_filter( 'wp_title', 'theme_slug_filter_wp_title' );
You can use filter hook
function theme_slug_filter_wp_title( $title ) {
if ( is_404() ) {
$title = 'ADD 404 TEXT HERE';
}
return $title;
}
add_filter( 'wp_title', 'theme_slug_filter_wp_title' );
try this one for wordpress version 4.4+
file : function.php
function theme_slug_filter_wp_title($title_parts)
{
if (is_404()) {
$title_parts['title'] = 'My Custom Title Text';
}
return $title_parts;
}
add_filter('document_title_parts', 'theme_slug_filter_wp_title');
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');
}
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.