Home url function not working correctly in plugin - wordpress

I have an issue that when i'm calling home_url($wp->request) in themes page or functions.php it works correctly, the out put is : http://localhost/WP_CUSTOM/tester , but when i'm calling in plugin the output is not correct : http://localhost/WP_CUSTOM .
This is i tried so far.
function init(){
global $wp;
$current_url = home_url( $wp->request );
print_r($current_url);
}
add_action('init','init');
I write these code in functions.php file of my plugin

That will depend on what it is that you are really trying to get.
global $wp;
home_url( $wp->request );
will generally give you the url of current page if the request is available. it is relative. ( a bit like get_permalink(), but not the same )
Also, in general, while in plugins it would be advised to use the wp get_*() functions, or in this case, get_home_url()
It would be better however if you will tell us what is thee url that you are trying to get and to what purpose. there might be better ways.

Related

How to detect current page in WordPress plugin development

I'm currently developing a plugin where I will restrict certain pages to the logged-in users only. I mean the normal pages we create in Wordpress.
I found this online but it does not seem to work for me.
function check_the_current_page(){
echo $slug = basename(get_permalink());
}
add_action( "init", "check_the_current_page" );
Another option I had was to detect current page id as shown in the code below. nothing gets printed on the screen, probably I'm using the wrong hook or something, someone help me
function check_the_current_page(){
global $post;
echo "Post id: ".$post->ID;
}
add_action( "init", "check_the_current_page" );
if only I'm able to get output in one of the above scenarios, then it would be very simple, but now nothing is being printed on the screen for both scenarios
Try This Code Work For Me global $post dosent work in init
function check_the_current_page() {
global $post;
echo "Post id: " . $post->ID;
}
add_action( "wp", "check_the_current_page" );

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

paginate_links() outputs "paged=" to link instead of "page="

I've been having a sever and completely unsolvable problem with pagination.
I did notice however this interesting point:
I search a keyword like cute:
?s=cute&submit=Search&post_type=image&paged=2
...is where the link leads to. "Paged=" gives 404 on random pages. But if I modify the URL to say page=
?s=cute&submit=Search&post_type=image&page=2
Every page destination works, bringing joy to my heart, but the pagination tab list always sticks at 0 (not reflecting current page).
I feel like I'm playing a "shell game" with code and settings, with wordpress bamboozling me.
Question reduces to:
How can I get &page= to be output in every case? Conversely, if &paged= should be the way it goes, how do I get it to work without 404s?!?!
This is a problem I've dealt with for almost 3 months now. People are shy to answer.
update:
Trying to deal with this paged variable in the URL which breaks pagination, I created a filter to simply replace it with page.
function symbiostock_pagination_mod( $args ){
$args = str_replace('paged=', 'page=', $args);
return $args;
}
add_filter( 'paginate_links', 'symbiostock_pagination_mod', 1 );
Would seem like a great idea! But then this happens with every new click:
?s=cute&post_type=image&page=2&page=2
?s=cute&post_type=image&page=2&page=3
So where is this being modded, and why?
update:
I'm not sure if I'm the only one that has ever had this problem, but I did solve it (at least for myself) you can see the solution below. Also thanks for the help given. :D
So here is what I came up with...we might as well call it a hack or work-around because its unknown if this was indeed a bug I'm dealing with (from wordpress itself) or just a perfectly hidden problem in my theme's code.
The below function filters the 'paginate_links()' function and achieves this:
I see paged variable generates 404 randomly on search pages, but works fine on archive and taxonomy pages. So, we check that. if_search(), we do our changes...
If this is a search, we get the would-be page # destination using regex (or simply string replace paged to page, depending on permalink structure. We use remove_query_var() to drop faulty variable, and add_query_var() to put the working one.
This is being done because in this strange case, page always generates proper results.
Then we return the edited destination link, and life is good.
Function below:
add_filter( 'paginate_links', 'my_search_pagination_mod', 1 );
function my_search_pagination_mod( $link )
{
if ( is_search() ) {
$pattern = '/page\/([0-9]+)\//';
if ( preg_match( $pattern, $link, $matches ) ) {
$number = $matches[ 1 ];
$link = remove_query_arg( 'paged' );
$link = add_query_arg( 'page', $number );
} else {
$link = str_replace( 'paged', 'page', $link );
}
}
return $link;
}
Also thanks to some help I got here with the regular expression: PHP Regular Expression Help: A quick way to extract the 2 from "page/2/"

Way to get Twitter button, with count, with custom bit.ly URL, working?

I'm stuck. I posted this on WordPress.StackExchange and they suggested I try at WebApps.StackExchange, and they suggested I try here. So, apologies for the multiple posts if you follow all those!
I have a client blog using bit.ly pro to generate custom short urls (ie foo.co). I want to show the regular horizontal version of the Twitter button, with tweet-count, and have the link that goes to the post use their custom bit.ly pro url.
I have installed Joost de Valk's Bit.ly Shortlinks plugin, which successfully converts normal WP shortlinks (wp_get_shortlink()) to the custom Bit.ly pro URL elsewhere in the site, but Twitter seems to trump that and render everything with the default t.co domain instead.
I've looked at the suggestions from this question but using the # as the data-url doesn't work, and the suggested Twitter support pages don't seem to contain any info on how to get Bit.ly to work (though they say they're going to).
Here's the function I created to insert the button in my theme - any ideas on where I'm going wrong? this is used to insert the button both within the Loop and on single-post pages.
function tweet_this() {
global $post;
ob_start();
$tweet = wp_get_shortlink();
echo '<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>Tweet';
return ob_get_clean();
}
In case it helps, this function does work, except it doesn't render the tweet-count:
function tweet_this() {
global $post;
ob_start();
$tweet = sprintf( __('%1$s %2$s'), $post->post_title, wp_get_shortlink() );
echo '<a class="tweethis" href="http://twitter.com/intent/tweet?text=' . urlencode( $tweet ) . ' via #clientname">Tweet this</a>';
return ob_get_clean();
}
Let me know if you need more info - and thanks in advance for any help you can throw my way!
Michelle
function tweet_this() {
global $post;
$tweet = get_permalink(); //replace with your code
$tweetmarkup = '<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>Tweet';
echo $tweetmarkup;
}
This works for me, but I don't have the WPShortlinks installed, so I replaced it with the permalink. You should be able to replace the permalink with your wp_get_shortlink and it should work.

Wordpress - How to include() a file that lies in another dir

I am writing a plugin that will take advantage of other plugin's features (think about a plugin for a plugin).
My file lies in /plugins/new-plugin/new-plugin.php
and I need to make a
include(/plugins/OLD_plugin/old-plugin.php)
so I can use a couple of functions from the old-plugin.php file.
What is the correct way to do this? I could maybe make the functions in old-plugin.php available globally, but I don't want to change the old-plugin.php file.
I've already tried several ways to do this, but none worked. The new-plugin will only show some info in an options page, not viewable for the general public and does not interact with any public page or post in my site.
I've already tried $_SERVER, WP_PLUGIN_DIR, WP_CONTENT_DIR, the absolute server path, relative paths and even some black magic, but nothing seems to work good.
With some of this solutions the plugin's options page shows good but the blog's pages do not render. With other solutions the inverse happens, and with some other solutions nothing even render, be it admin pages or blog's pages, all with errors regarding to file not found.
The new-plugin.php is as simple as
<?php
/*
WP Common Headers
*/
global $wpdb;
if ( ! defined( 'WP_CONTENT_DIR' ) )
define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
if ( ! defined( 'WP_PLUGIN_DIR' ) )
define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
include '/server-absolute-path/public_html/gameblogs/wp-content/plugins/old-plugin/old-plugin.php';
add_action('admin_menu', 'new_plugin_menu');
function new_plugin_menu() {
$page_title = 'New Plugin';
$menu_title = 'New Plugin';
$function = 'new_plugin_admin_page';
$menu_slug = 'new_plugin';
add_menu_page($page_title, $menu_title, 0, __FILE__, $function);
}
function new_plugin_admin_page() {
$result = old_plugin_link_data(" WHERE link_destination NOT LIKE '/%' AND link_destination NOT LIKE '%gameblogs%'");
$total = count($result);
old_plugin_list_links($result, $total, FALSE, FALSE);
*/
}
?>
thanks for any ideas!
check the old plugin files and see if there are any do_actions or apply_filters in it. If there are then you can hook into the old plugin script with your new plugin using add_action and apply_filters and execute other things you want to do.
see http://codex.wordpress.org/Function_Reference/do_action
and http://codex.wordpress.org/Function_Reference/apply_filters
For example (very basic example):
If in old plugin you find a:
do_action('some_type_of_reference);`
In your new plugin you can hook into it by doing:
`add_action('some_type_of_reference', 'name_of_my_function');
function name_of_my_function() {
//executed code here
}`
If in old plugin you find a:
apply_filters('some_type_of_reference', $variable);
Then in your new plugin you can hook into the filter by doing:
apply_filter('some_type_of_reference', 'my_function');
function my_function( $variable ) {
//act on the variable from the filter.
return $variable;
}
Have you looked at the plugins_url function? I haven't had an in-depth read through your code, but it might help.
The plugins_url template tag retrieves the url to the plugins directory or to a specific file within that directory. You can hardcode the plugin slug in $path or pass FILE as a second argument to get the correct folder name.
Hope this helps!

Resources