How to get the whole html content when visiting a page of splited wordpress post - wordpress

I am doing string conversion for every post/page in Wordpress. I add below function to template_redirect hook to get the whole html content and do the conversion.
function my_string_conversing() {
obstart( 'ob_callback' );
return;
}
function ob_callback( $buffer ) {
// doing conversion here...
return $buffer
}
add_action( 'template_redirect', 'my_string_conversion' );
These code works well for posts and pages, but if I split one post into multiple pages, then this will not work. It even don't run my_string_conversion function when click each page of a split post. How can I make it work? Thanks.

I found the solution, use add_action( 'template_redirect', 'my_string_conversion', -100 ); instead.

Related

how to remove "added-to-cart notice" from every page except product archive pages?

Note: In looking for an answer to my question I came across this post but it is NOT duplicate: Remove add to cart notice and change "add to cart" button in Woocommerce the answer there gives the option to remove the notice from the entire site. I want to remove it only from the cart page and I don't want to do it with CSS.
I use external links to my site to send people directly to the shopping cart with the item already added to the cart. When doing so, the "added-to-cart notification" shows up on the cart page which I do not want.
I found this code which removes the added-to-cart notification: add_filter( 'wc_add_to_cart_message_html', '__return_false' ); but it removes the notification from all pages of my site which is not what I want.
To be more specific, I want the added-to-cart notification to show on every product archive page and nowhere else.
I tried to add a filter but it doesn't work the way I would expect it to, I tried the following two ways (and tested it with various pages to see if I could make anything work but it seems my general syntax is off because I Can't get it to do anything...
function hide_cart_notes() {
if ( ! is_archive() ) {
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
}
}
add_action( 'woocommerce', 'hide_cart_notes' );
function hide_cart_notes() {
if ( is_archive() ) {
return;
}
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
}
add_action( 'woocommerce', 'hide_cart_notes' );
when woocommerce hook starts? where it's docs? does it run at all?
these question should be answered before.
i know that WordPress parses query at parse_query hook, so i would try this
add_action('parse_query', function() {
if (!is_archive()) {
add_filter( 'wc_add_to_cart_message_html', '__return_false' );
}
});
because is_shop(), is_archive(), is_* need query to be parsed first.

Where exactly is the "Before Header Content hook" in Wordpress?

I want to add a little code to the "Before Header Content hook" but I don't know where that is... Can you please help me?
Try:
add_action('init', 'process_post');
function process_post()
{
echo "test";
}
this is a modified version of #ajay 's solution
if you are going to use this, then you have to make sure that the current user is not an admin using is_admin() function.. and only display it if he is not an admin ...
why !?
because if you didn't, it may mess up the wp-admin of your website..
add_action('init', 'process_post');
function process_post()
{
if (!is_admin()) {
echo "test";
}
}
This could be tricky simply because every theme differs with how the loop is displayed, however you could create a plugin to use the loop_start action, which is called before the first post of the standard WP loop:
add_action( 'loop_start', 'test_loop_start' );
function test_loop_start( $query ){
echo 'this is my inserted text';
}
Now using this would display it every single time the loop is called (whether on a page, a post, category page, search page, etc.), which you may not want.
add_action( 'loop_start', 'test_loop_start' );
function test_loop_start( $query ){
if(is_category() OR is_singular()) {
echo 'this is my inserted text';
}
}

WooCommerce Registration Shortcode - Error messages problems

I am currently creating a widget to display the registration form on a WordPress website that uses WooCommerce. For now, I only have 3 basic fields which are email, password, repeat password. I'm looking forward to add more WooCommerce fields, but want to solve that problem before jumping to the next step.
I'm having some problems with the messages output (wrong password, account already exists, etc).
I searched on the web and there was no shortcode already built for WooCommerce registration, beside their registration page. So I went ahead and created a shortcode, with a template part.
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
This is a snippet I use to get the template part inside a variable, since the default function would "echo" the content instead of "returning" it.
function load_template_part($template_name, $part_name=null) {
ob_start();
get_template_part($template_name, $part_name);
$var = ob_get_contents();
ob_end_clean();
return $var;
}
So, the problem is, when I call woocommerce_show_messages or $woocommerce->show_messages(); from my template part, nothing is showing, or if it is, it shows at the top of the page.
I did try to put the calls inside my shortcode function:
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$woocommerce->show_messages();
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
Doing so, the message output inside the <head> tag, which is not what I want.
I tried to do the same trick with ob_start(), ob_get_contents() and ob_clean() but nothing would show. The variable would be empty.
I also did try to hook the woocommerce_show_messages to an action as saw in the core:
add_action( 'woocommerce_before_shop_loop', 'woocommerce_show_messages', 10 );
For something like:
add_action( 'before_registration_form', 'woocommerce_show_messages');
And I added this in my template-part:
<?php do_action('before_registration_form'); ?>
But I still can't manage to get the error messages show inside the box. It would always be inserted in the <head>
I will share final solution when everything is done.
Thanks for your time,
Julien
I finally got this working by hooking a custom function to an action which is called in my header.php
I guess hooking functions inside template part does not work as intended.
In header.php, I got this:
do_action('theme_after_header');
And here's the hooked function. Works perfectly.
function theme_show_messages(){
woocommerce_show_messages();
}
add_action('theme_after_header', 'theme_show_messages');
However, I will look into 'unhooking' the original show message function since it might show twice. Need to test some more ;)
You can also just use the [woocommerce_messages] shortcode in your template where you want it displayed
Replying to a bit of an old question, but you can also try the following:
$message = apply_filters( 'woocommerce_my_account_message', '' );
if ( ! empty( $message ) ) {
wc_add_notice( $message );
}

Wordpress Plugin: Show html only on standard page and not in admin area

I'm writing a plugin and I need to display a piece of text in the WP page, but not in the admin area. How can I do so?
I tried this in the construct:
add_action( 'init', array( $this, 'initPage' ) )
and then:
public function initPage() {
echo 'hello';
}
but the text is displayed also in the admin area. Is there a way to do this? It would be the opposite of the action admin_init I assume.
Proper way to handle it: is_admin()
http://codex.wordpress.org/Function_Reference/is_admin
if(is_admin()) { // do nothing } else {
// function you want to execute.
}
I solved this by adding it to a shortcode action. Like this:
add_shortcode( 'myPlugin', array( $this, 'shortcode' ) );
and:
public function shortcode( $atts ) {
return 'hello';
}
With the above code, 'hello' will only display on the front-end. Not sure if that's the cleaner way to do it, but does the job.
There is no "front-end-only" version of init, however you probably don't want to be doing any output at the init action anyway.
What exactly are you trying to do? Usually, you use an action hook for specific types of things, and causing output very early at something like "init" is rare and weird.

How to deactivate past shortcodes in Wordpress

Recently I changed the theme of my site, and I found many of my articles use a shortcode like this
[box]
....
[/box]
My new theme does not support it and I actually don't need this shortcode to function. I thought I could just write a empty function for the shortcode in function.php, like this
function shortcode_box() {
return "";
}
add_shortcode('box', 'shortcode_box');
but it's not working.
Do you know any method to deactivate this short code?
So, you want to leave the [box] bits in the posts and/or pages, but have them not do anything? Try a shortcode that passes through the content unchanged:
function shortcode_box( $atts, $content = null ) {
return $content;
}
add_shortcode( 'box', 'shortcode_box' );
(For enclosing shortcodes, the return value of the function is used to replace the entire shortcode.)
Use remove_shortcode()
remove_shortcode('box');
Reference: http://codex.wordpress.org/Function_Reference/remove_shortcode

Resources