Wordpress – Country Sensitive Post View - php

I'm looking for a solution to display posts based on the viewers
location.
For Example: I'm writing an article and set an option so it is only
visible for users from within USA.
If a user in Germany is visiting the Wordpress page, the post does
not appear.
I don't want to change content within a post, but more hide and show
them with an option for the author.
If there is some plugin, that can do this, I would highly appreciate.
Thanks.

I had similar problem. Didn't found a plugin and wrote this to query post for country of visitor: (remember to get your key at http://ipinfodb.com/)
$ip=$_SERVER['REMOTE_ADDR'];
$request = 'http://api.ipinfodb.com/v3/ip-country/?key= get your own key &ip='.$ip.'&format=json';
$response = file_get_contents($request);
$jsonobj = json_decode($response);
if ($jsonobj->countryCode == "SE"){
// args for query post for selected country $args = array ()
}
elseif ($jsonobj->countryCode == "PL"){
// args for query post for selected country $args = array ()
}
query_posts( $args );

Have a look at this discussion over here: https://wordpress.stackexchange.com/questions/26832/display-only-certain-posts-based-on-visitors-country. It might not give you a ready-to-use solution but maybe it helps as a pointer in the right direction.

Related

How to get full info from given site url in Wordpress

I need to get full info by given site url.
For example:
$href = 'http://example.com/news/news-1';
I can get post id by
$post_id = url_to_postid( $href );
Then I can get post type by
$post_type = get_post_type( $post_id );
But it gives me nothing if it's a category url. So is there any other way to get info on given site url? Is this page even exist? Is it a page or a post? Is it a category?
You should use get_queried_object it will returns the queried objevt type, id etc.
Here is an example:
$current_item = get_queried_object();
A lot of is_ functions help you to determine type of node (is_single, is_category etc - check https://developer.wordpress.org/reference/functions/page/31/ and next pages). Then you get all info about this node.

Wordpress API - link to full post details from list of posts

I have installed the Wordpress API onto on of our company websites so that we can retrieve data from it to display on a new company website. With regards to grabbing and displaying the initial data all works fine (posts from a specific category from the original website onto a specific page on the new website).
This is for a events that are input onto a 'whats on' page on the existing website that take place at a bar/restaurant. The new site is purely dedicated to the events and to save having to add/amend the events on both sites we thought it best to use the API.
To retrieve the full listing of events (which works) I use:
$response = wp_remote_get( 'http://restuarantdomain.com/wp-json/wp/v2/posts?filter[category_name]=live-events&per_page=50' );
I also know that to display an individual post I need to use:
$response = wp_remote_get( 'http://restaurantdomain.com/wp-json/wp/v2/posts?filter[name]=event_name );
What I can't work out how to do is create a 'friendly' url on the new site that will then go to a page that calls the function to get the individual post details.
For example I want the url http://eventdomain.com/whats-on/event-name (in the list of event posts grabbed through the 1st API call above) to go to the page What's On and on that page use the event-name from the url as the parameter in the 2nd API call above. But Wordpress just looks for the category and post name due to the format of the url which don't exist so I just get a 404.
What am I missing?
You must create your custom Endpoints.
for instance, if you want to filter all posts within a particular category you should do this
function my_cat_func( $data ) {
$posts = get_posts( array(
'cat' => $data['id'],
) );
if ( empty( $posts ) ) {
return null;
}
return $posts[0]->post_title;
}
then you must add an action to register your endpoint call
add_action( 'rest_api_init', function () {
register_rest_route( 'myplugin/v2', '/categoy/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_cat_func',
) );
} );
then you will get something like this
http://example.com/wp-json/myplugin/v2/categoy/(?P<id>\d+)
for more information and better understanding, I suggest you reading this reference:
http://v2.wp-api.org/extending/adding/
This article also can give you more example: https://webdevstudios.com/2015/07/09/creating-simple-json-endpoint-wordpress/
and
https://deliciousbrains.com/wp-rest-api-customizing-endpoints-adding-new-ones/
Note: I wrote this code on the fly, that should need more efforts to work.

How to filter posts by author on edit.php

Let's suppose the user visits the page of
http://mywpsite/wp-admin/edit.php?author=john-doe
Let's suppose there is an author with the name of 'John Doe' and the given author has three posts. Yet, when I visit the page, I see an empty grid, as if there were no posts created by this author.
I would like to search for posts created by the given user. Based on my research, I can see that people are claiming that something like this should work:
function posts_for_current_author($query) {
if($query->is_admin) {
global $user_ID;
$query->set('author', $user_ID);
}
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');
Source.
However, here the author's ID is expected as input, yet, I do not know how to get the author's id by the slug of 'john-doe'.
How can I get the ID of the author by slug and search for posts based on that ID?
EDIT:
This is one failed try, based on NATH's comment:
function wpshock_search_filter( $query ) {
if ((is_admin()) && (isset($_GET["author"])) && (preg_match('/[^a-zA-Z_0-9]/i', $_GET["author"]))) {
$query->set("author_name", $_GET["author"]);
}
return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');
The query still returns no elements. I have var_dumped $query and seen that $query->query["author"] has the textual problem, which is a potential problem. Also, $query->tax_query contains data related to author. I am sorry if this question is worthy of down-votes, I thought others might be confused by Wordpress's database handling as well and thus this question might be useful. Maybe I was wrong.
This should work for you :
get_user_by('slug','john-doe');
EDIT :
From this you can get author object and you can carry forward with your code.
Link
WordPress provides an author_name query variable to you. You can use the following by default:
http://mywpsite/wp-admin/edit.php?author_name=john-doe
Read more about public query variables in the Codex.
I'm wotking on wp front-end and reach similar feauture you are requested
The code you provide are woking current on wp-admin page but any one uses that code prevent his own SuperAdmin account form reviewing other editors posts .
just add !is_super_admin() to your first contition like this :-
if($query->is_admin && !is_super_admin() )
BTW the author's ID are already provided with global $user_ID;
and when you add this $query->set('author',$user_ID); the main query will filter posts result accurding to author ID , see WP_Query();
See this-
https://wordpress.stackexchange.com/questions/89990/how-we-can-get-the-author-id-by-its-name
And, a better way to get posts by an author would be-
get_posts('author'=>AUTHOR_ID);

Why does WordPress add a duplicate numeric slug upon post/page view?

This is my first post here, so I apologize in advance for any mishaps.
I've been searching for hours trying to figure this one out, but I simply can't seem to understand why this is happening.
The site I'm setting up is a child site (not in a multisite sense, but as a separate site/domain with the same branding). Some of the posts on my site will originate from the parent/main site (but will be made as new posts through copy-paste), and I want the original article ID as part of the permalinks.
E.g. http://www.example.com/hello-world/12345/, where 12345 is the article ID of the article on the parent/main site.
To accomplish this, I've added a custom field to my posts where I can add the article ID of the original article with external_article_id as Field Name. I've then tried to manipulate the permalinks with the following code:
add_filter('post_link', 'append_custom_permalink', 10, 2);
function append_custom_permalink($url, $post) {
$newurl = $url;
if ($post->post_type == 'post') {
$custom = get_post_custom_values('external_article_id', $post->ID);
if (!empty($custom))
$newurl = $url . $custom[0] . '/';
}
return $newurl;
}
Whenever I output the permalink to the posts it appears exactly as I want it, both in the editor and on the site. However, when I either click a link or enter the address manually, I get redirected automatically to http://www.example.com/hello-world/12345/12345/. It duplicates the additional numerical slug, and also happens when I replace $custom[0] with a hard-coded numeric value. This applies to all posts, and my permalink structure (in the settings) is set to /%postname%/.
I even tried setting the permalink structure to /%postname%/%ext_article_id%/ and replace %ext_article_id% with $custom[0], but with the exact same outcome. I also tried using the same code on another WordPress site, except this time with pages instead of posts, also with the exact same outcome.
Ideally I would like to use something like add_query_arg($custom[0], '', get_permalink($post->ID));, but omit the question mark that comes along with it.
Could someone please explain to me why this is happening, and how I can circumvent this? Do I need to use some other filter, or how can I approach this?
Thank you in advance!
In order to make this work you also need to make WordPress aware of the rewrite_tag and specify an additional permalink structure via add_permastruct. The following code should do the trick:
function append_custom_permalink( $post_link, $post ) {
if( $post->post_type == 'post' ) {
$custom = get_post_custom_values( 'external_article_id', $post->ID );
if( ! empty($custom) ) {
$post_link = $post_link.$custom[0].'/';
}
}
return $post_link;
}
add_filter('post_link', 'append_custom_permalink', 10, 2);
function sof_30058470_posts_rewrite() {
add_rewrite_tag('%external_article_id%', '([^/]+)', 'external_article_id=');
add_permastruct('external_article_id', '/%year%/%monthnum%/%day%/%postname%/%external_article_id%/', false);
}
add_action('init', 'sof_30058470_posts_rewrite', 10, 0);
Make sure to re-save your permalink structure at Settings->Permalinks once you added the code. You may also need to refresh/clear your browser cache.

Get store ID that a page belongs to

I'm trying to find out what store a page belongs to in Magento. We currently have along these lines:
$cms_pages = Mage::getModel('cms/page')->getCollection();
$cms_pages->load();
foreach($cms_pages as $_page) {
$data = $_page->getData();
}
How would I get the store ID for each page? Ideally I'd like something as simple as $data->store_id(); but I've not found anything of use yet. Can anyone point me in the right direction?
Add Store id to your collection and it will only return the pages you need:
$collection = Mage::getModel('cms/page')->getCollection()
->addStoreFilter(Mage::ap‌​p()->getStore()->getId())
->setOrder('sortorder', 'asc');

Categories