I have tried to implement the code which can get the user region when the user open the page but it is not working i have used this code inside the functions.php
function user_region() {
$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));
return '<div class="user-region">'.$details->region.'</div>';
}
add_shortcode("get-user-region", "user_region");
this is my shortcode inside the functions.php and i have used the shortcode inside the wordpress page
<?php echo get-user-region_callback(); ?>
<?php echo do_shortcode('[get-user-region]') ?>
I have tried both ways but both are not working no results are showing from this shortcode.
First of all, your URL to ipinfo is incorrect. If are treating the response as JSON, but the output of https://ipinfo.io/ip is HTML. You need to specify/json` at the end:
https://ipinfo.io/{$ip}/json
Other than that, you need to check that file_get_content is enabled on your platform and that it allows remote url loading (setting allow_url_fopen).
Related
I'm making a plugin that will access a resource on different accounts.
I have made entries in wp-config.php as folow:
define('account_key_1','3452345dfg')
define('secret_1','123123')
define('account_key_2','3452345dfg')
define('secret_2','123123')
I would like to get the account detail into a variable based on a plugin shortcode settings
like
[myplugin account="1"]
would make the code use account 1
I have setup the code to get the value of the shortcode tag and it works and echo $account print whatever i put in the shortcode under account.
I have tryed to use
$key= accout_key_.$account
Put that sent it to "account_key_1"
if i do
$ket= account_key_1;
it works, but i would like to change the ket number dynamically based on the shortcode.
Any Ideas?
Your question wasn't that clear for me but based on your example, the below will print 3452345dfg
$account = 2;
$key = constant('account_key_'.$account);
echo $key;
Reference
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.
I creating a small plugin to add <!--nextpage--> shortcode under some requirements.
For example , my plugin will check if the user agent is a bot, if yes it will not add <!--nextpage--> and if not it will add it.
What I achieved so far is I created a function to check user agent :
function test(){
if($_SERVER['HTTP_USER_AGENT'] == ' Bot i want '){
// here we dnt show <!--nextpage-->
} else {
do_shortcode('<!--nextpage-->');
}
}
add_shortcode('myshortcode','test');
So when I add the short code in the post editor it work fine, but the only problem is it doesn't add the <!--nextpage-->, it doesn't get executed, and if I try other short codes like [tweetme] or other registred short codes they work just fine.
To print out into your html page, you shouldn't not call
do_shortcode('<!--nextpage-->');
The above line will do nothing, since is not a valid shortcode format in Wordpress, and as a result, do_shortcode cannot parse it.
A valid shortcode in wordpress need to be wrapped with [ ] brackets.
To achieve what you are trying to do (printing out '' when web crawler detected), you can just change your test function to:
function test(){
if($_SERVER['HTTP_USER_AGENT'] == ' Bot i want '){
// here we dnt show <!--nextpage-->
} else {
echo '<!--nextpage-->';
}
}
Also, you can have a look at do_shortcode function documentation here: https://developer.wordpress.org/reference/functions/do_shortcode/
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.'"]' );
?>
I have a custom php page outside of wordpress, this page is connecting also to a external database, and get resultat form a sql query on a variable
i use require('../wp-blog-header.php') and require('../wp-load.php') to integrate with wordpress.
now i need to use this variable on the functions.php of my wordpress theme.
i do on my external page :
<head>
<connect to my database>
<my query>
<get resulte on $variable>
<require('../wp-blog-header.php')>
<require('../wp-load.php')>
but when i use $variable on my functions.php it do not work ?
You need to include wp-load.php alone. It will load wp-include (and wpdb), plugins and your theme (functions.php). To use the database directly you would do this:
global $wpdb;
$wpdb->get_results('select ...');
If you need to access variable declared in functions.php, it's straight forward:
say.php
<?php
include 'var.php';
print_r( $moo );
?>
var.php
<?php
$moo= 'cow';
?>
output of say.php
$ php say.php
cow
Loading (include or require) functions.php after wp-load.php will result in redeclaration of the functions defined within functions.php (it will be loaded twice), causing PHP to crash.
Unless you are using wordpress features in your external query there is no need to include wordpress files.
Your external.php could look something like
function handy_prefix_my_processing_function()
{
//connect to database...
//Do something with data...
$result = $results
//return the result
return $result;
}
then in functions.php
function handy_prefix_get_my_variable()
{
//Load the external file and make its functions available
include_once( 'external.php' );
//Get my result
$result = handy_prefix_my_processing_function();
//Do something with the result....
}