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

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.

Related

How to use conditional add_action for Wordpress

I am having a small issue where i need to call add_action method which is conditional. Basically here, first checking whether the plugin active or not by admin_init action. If true then i need another add_action call from that action.
Please find the scenario what i am looking for:
function check_some_other_plugin() {
if ( is_plugin_active('some-plugin.php') ) {
//if true then vc_before_init action will run
add_action( 'vc_before_init', 'vc_master_home_slider' );
}
}
add_action( 'admin_init', 'check_some_other_plugin' );
function vc_master_home_slider() {
//my stuff
}
I am really eager to know how this things can be possible. Thanks in advance.

How can I use is_page() inside a plugin?

I want my plugin to register a script only in a certain page.
For example, inside my plugin file I want to write something like this:
if (is_page()) {
$pageid_current = get_the_ID();
$page_slug = get_post($pageid_current)->post_name;
if ($page_slug == 'articles'){
wp_register_script('myscript', '/someurl/main.js');
}
}
But I get the error:
is_page was called incorrectly. Conditional query tags do not work
before the query is run. Before then, they always return false. Please
see Debugging in WordPress for more information. (This message was
added in version 3.1.)
How can I, inside of a plugin, register a script in a certain page?
is_page() only work within template files.
And to use it within plugin files, you need to use it with the combination of template_redirect action hook.
This action hook executes just before WordPress determines which template page to load.
So following snippet would work:
add_action( 'template_redirect', 'plugin_is_page' );
function plugin_is_page() {
if ( is_page( 'articles' ) ) {
wp_register_script( 'my-js-handler', '/someurl/main.js', [], '1.0.0', true );
}
}
You could use is_page() after template redirect so you need to add in the hook like this :
add_action('template_redirect','your_function');
function your_function(){
if ( is_page('test') ) {
// do you thing.
}
}
You must register your script as if you want it to work everywhere.
You can de-register it after the job is done, like this:
function deregister_my_script() {
if (!is_page('page-d-exemple') ) {
wp_deregister_script( 'custom-script-1' );
}
}
add_action('wp_print_scripts', 'deregister_my_script', 100 );

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

How do you remove or change the functionality of the Publish button on a custom WordPress post?

I have a custom post type and need keep the post status from getting set to 'Published' when you click the Publish button. Instead, it should work like the Save Draft button. So I either need to figure out how to just remove the Publish button so the user's can only click Save Draft our preferably, update the Publish button functionality so it doesn't set the post to publish.
You can use wordpress action hooks to modify default behaviors.
http://codex.wordpress.org/Function_Reference/add_action
In your case, you want to use the 'publish_post' hook.
So you can do
function dont_publish( $post_ID )
{
if(get_post_type($post_ID) == 'your_custom_type'){
exit;
}
}
//the dont_publish function will be called after the publish button is clicked
add_action( 'publish_post', 'dont_publish' );
The way it is above, nothing will happen at all if the publish button is clicked, but you can play around with the dont_publish function to get the results you want.
#PhoenixWing156 was close but one little change so the the other post types get updated as usual.
function dont_publish( $data , $postarr ) {
if($data['post_type'] == 'custom_post_type') {
$data['post_status'] = 'draft';
}
return $data;
}
add_filter('wp_insert_post_data' , 'dont_publish' , '99', 2);
The wp_insert_post_data hook is called before information about a post is saved to the database.
http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data
You can try:
function dont_publish( $data , $postarr )
{
if($data['post_type'] == 'custom_post_type'){
$data['post_status'] = 'draft';
return $data;
}
}
add_filter('wp_insert_post_data' , 'dont_publish' , '99', 2);
WordPress provides the remove_meta_box() function exactly for this purpose.Just add this below code:-
add_action( 'admin_menu', function () {
remove_meta_box( 'submitdiv', 'Your_custom_post_type', 'side' );
} );
You could also disable the default saving metabox and add you own.
This is not documented well in the developer docs of wordpress.
To do this you have to hook into the "add_meta_boxes"-hook and in the hooked function yo have to call remove_meta_box('submitdiv','your-cpt','side');
The code should be something like this:
function your_cpt_metaboxes(){
remove_meta_box('submitdiv','your-cpt','side');
...
}
add_action('add_meta_boxes','function your_cpt_metaboxes');
your-cpt has to be changed to the name of your cpt of course.
I was also searching for this handy snippet and found it in the plugin Awesome Support.
The original saving metabox code can be found in /wp-admin/includes/metaboxes.php .
Just search for post_submit_meta_box (in WP 5.4 on line 22).

Custom post type functions.php if statement on action

I am using developing a child theme for Woothemes' Canvas.
I am trying to use functions.php in the child theme to only use actions on my custom post type.
This code doesn't seem to be working:
add_action( 'woo_post_inside_after', 'my_geo_mashup' );
function my_geo_mashup() {
echo GeoMashup::map();
if ($post->post_type == 'listings') {
//My function
}
}
add_action( 'woo_post_inside_before', 'listings_nivo' );
function listings_nivo() {
echo do_shortcode('[nivo source="current-post" ]');
if ($post->post_type == 'listings') {
//My function
}
}
So, I'm unsure how to get the above to work properly and only show these items on the custom post type, or only for the custom post type template single-listings.php (as I only want the map and slider to show on the actual post, not on the blog page (archive.php)
Rather than making the entire $post object global, you can just make $post_type global instead. Ex below.
I'm not exactly sure where that function is being loaded, but make sure you hook somewhere within the post. If the action is before, as far as I know and from experience, the post variable will be null.
Just as a test, try running the action in wp_footer Ex. add_action( 'wp_footer', 'listings_nivo' );
See if that yeilds any results.
if echoing var_dump($post) is still null, well, not sure where to go from there.
So you can try running the below, then run the action in the appropriate place if it works:
function listings_nivo() {
echo do_shortcode('[nivo source="current-post" ]');
global $post_type;
// Diagnostic purposes
echo var_dump($post_type);
if ($post_type == 'listings') {
//My function
}
}
add_action( 'wp_footer', 'listings_nivo' );
Check your error log or turn wp_debug to true in your wp-config.php file if nothing else to see if anything else is going on.
Best of luck!
Inside your function, try adding global $post;. Then to see what you are getting with $post->post_type echo it out to the screen. As long as this gives you "listings", your code should work. If not, there's probably another issue at play.

Resources