Buddypress overriding author url - php

I am getting author's url on Wordpress from outside the loop using this:
<?php
get_author_posts_url( $author_id, $author_nicename );
?>
Which works fine by delivering the author URL in this format -
mysitename.com/author/john-james.
However when Buddypress is enabled, this same URL changes to
mysitename.com/members/john-james
Is there a way to prevent this from happening?

get_author_posts_url() calls $wp_rewrite->get_author_permastruct() which by default returns:
$this->author_structure = $this->front . $this->author_base . '/%author%';
The WP_Rewrite class has this filter which may let you change the values:
apply_filters( 'author_rewrite_rules', array $author_rewrite )
All this is info is from the WP Codex.

I eventually have to use this code - <?php echo get_site_url(); ?>/author/<?php echo($user_name); ?> where $user_name = $user_info->user_nicename;
This also gets me - mysitename.com/author/john-james
There may be other ways, but this allows me to use the two URLs together for different purposes, one to check the author's profile (through buddypress) and the other to view all author's articles.

Related

Variable in do_shortcode to build google map does not work

I'm working on wordpress, using two plugins - one to manage custom post types and meta fields, and another one to display google maps with a shortcode. I try to get the address from a custom field, store it into a variable and then use it with do_shortcode:
<?php $address = get_cfc_field('infos', 'anschrift');
$address1 = 'Auenstraße 29 80469 München';
// Same output in html for these two...
echo $address;
echo $address1;
// But this one only works with $address1
echo do_shortcode( '[display_map address="'.$address1.'"]' );
?>
There are two variables for testing. Both variables give me the same output when echoed. So get_cfc_field seems to work fine.
But the Shortcode works only with $address1, with $address the map won't display the location. I already tried to strip_tags and esc_html but that did not make any difference.
What am I overlooking?
The code as posted was fine, my problem was an invalid api key for the google maps plugin I used...
Interesting and irritating is only the fact, that the api responded sporadically shortly after I installed the plugin. (with address 1 only).
So this actually works as expected:
<?php $address = get_cfc_field('infos', 'anschrift');
echo do_shortcode( '[display_map address="'.$address.'"]' );
?>

Multiple pages with same URL in WordPress

I have a WordPress site that has a standard Page called Places with URL
example.com/places/
And I have several child Pages called by cities
example.com/places/city-1
example.com/places/city-2
Now, I have a custom post type Place that should indicate a single place and should have permalink like
example.com/places/place-1
But then if I go to one of the previous links with city-1, city-2 I get 404 obviously because there is no place with that permalink.
Is there a way for WordPress to drop to previous permalink. So if there is no place with that name, look for a page with it.
You could probably use the the REFERER-value from the PHP server variable $_SERVER, but it´s not very reliable and can be altered.
I am using the plugin "Permalink Finder" in one of the pages I am maintaining and that works quite well for finding changed URL. You could give it a try and see if it works for you, too.
In case somebody ever having a similar problem, it can be done by using verbose page rules. This is an example I found at WordPress Stack Exchange
https://wordpress.stackexchange.com/questions/22438/how-to-make-pages-slug-have-priority-over-any-other-taxonomies-like-custom-post
add_action( 'init', 'wpse16902_init' );
function wpse16902_init() {
$GLOBALS['wp_rewrite']->use_verbose_page_rules = true;
}
add_filter( 'page_rewrite_rules', 'wpse16902_collect_page_rewrite_rules' );
function wpse16902_collect_page_rewrite_rules( $page_rewrite_rules )
{
$GLOBALS['wpse16902_page_rewrite_rules'] = $page_rewrite_rules;
return array();
}
add_filter( 'rewrite_rules_array', 'wspe16902_prepend_page_rewrite_rules' );
function wspe16902_prepend_page_rewrite_rules( $rewrite_rules )
{
return $GLOBALS['wpse16902_page_rewrite_rules'] + $rewrite_rules;
}

Set Facebook wordpress plugin fields while using wp-insert-post

I am using wp-insert-post to create posts dynamically in wordpress. The script is in an external file and I have included the wp-load.php file to make use of the wordpress functions there. My query is that while using the wp-insert-post function, we can make use of the default parameters, but how can we include the parameters that are used by the plugin (Facebook, in this case).
I wish to set this parameter: facebook_page_message_box_message
I tried using:
$post = array();
$post['post_type'] = 'post';
$post['post_title'] = 'Custom title';
$post['post_content'] = 'Custom content';
$post['post_author'] = '1';
$post['facebook_page_message_box_message'] = 'Custom Title (to be displayed on the Facebook timeline, when the post is published)';
$post_id = wp-insert-post($post);
But this is not working. I also tried using wordpress functions like,
Wp_set_object_terms, but even that didn't work.
https://github.com/facebookarchive/wordpress/blob/master/admin/social-publisher/publish-box-page.php
The following links may provide additional information

I need to pass a variable between files using PHP in Wordpress

I have created two plugins. Plugin A gets the latest posts, all of them. It has an option that can remove posts by entering their ID, this is accessible via the shortcode or directly in functions.php. This works.
Plugin B gets the most popular posts using an internal analytics systems. I have a variable called $most_popular_ids which contains all of the post ID's of the highest viewed posts for that particular category. So $most_popular_ids changes depending on which page it is on. This works.
These are displayed side by side on the main pages of the website. So ideally I would like them to not show duplicate posts.
What I finally need to do is pass $most_popular_ids from plugin B to plugin A or to functions.php. This will allow me to exclude all of the most popular posts from the latest posts.
Obviously making the variable global only works in the scope of the file so that won't work. I tried creating a $_SESSION but you can't do that in Wordpress as far as I know. Most of the stuff in the plugins can't be redeclared so my include attempts didn't work either :\
Can anyone help me?
You can use sessions in WordPress, but it would probably be easier to use the wp_cache_set() and wp_cache_get() functions
For example, in Plugin B you would do something like:
wp_cache_set('mykey_most_popular_ids', $most_popular_ids);
Then in Plugin A or your functions file you can do:
$most_popular_ids = wp_cache_get('mykey_most_popular_ids);
Obviously you'd need to make sure the function in Plugin B sets the cache entry before trying to get it in Plugin A of the functions file
Here's an approach using filters. In plugin A, implement a filter that allows to set the post__not_in param of the query:
$query_args = array();
$post__not_in = apply_filters( 'check_for_most_popular_ids', false );
if( $post__not_in && is_array( $post__not_in ) )
$query_args['post__not_in'] = $post__not_in;
$query = new WP_Query( $query_args );
In plugin B, hook into the filter and pass the most popular IDs:
add_filter( 'check_for_most_popular_ids', 'send_most_popular_ids', 10, 1 );
function send_most_popular_ids( $post__not_in = false )
{
$most_popular_ids = get_most_popular_ids(); // returns array of IDs OR false
if( $most_popular_ids )
return $most_popular_ids;
return $post__not_in;
}

Wordpress custom query

I have a wordpress blog. I created a db table which stores dictionary information and I want to publish this data from a URL . (For ex: "myblogaddress.com/mytest.php")
I have been researching for 2 days but nothing works I tried.
In my page; I use the php code shown in blow.
<?php
global $wpdb;
$words = $wpdb->get_results("SELECT * FROM $wpdb->words")
echo $words[0]->ENG;
?>
I wonder that;
- Which directory does my php page to be into ?
- What I need to do (other config, permission etc.) to do what I want.
Regards.
If you're loading it from a standalone PHP file (ie not from within your WordPress theme), you'll have to call wp-load.php to initialise the WordPress variables (including $wpdb). Have a look at this answer, including the comment about only needing wp-load.php.
I'd consider using a relative path (what that would be would depend on where you put your page relative to WordPress) rather than using $_SERVER['DOCUMENT_ROOT'];, but that's just a personal preference.
EDIT
Rereading after seeing your comment, I've just realised $wpdb->words probably won't exist. Try
$words = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "words")
instead. That'll generate the table name correctly as wp_words. Of course, you'll need to populate it the same way.

Categories