Here i am trying to pass amount from shortcode to the url. Whenever i try to change the value of amount from shortcode it's not changing. It shows the default value only i.e of amount 1
Here's the code
function currency_conversion_shortcode($atts)
{
$atts = shortcode_atts( array(
'amount' => '1',
), $atts, 'currency_conversion' );
$url = "https://api.fastforex.io/convert?from=AED&to=NPR&amount=" . $atts['amount'] . "&api_key=xxxxx-xxxx-xxxx";
$result = file_get_contents($url);
$result = json_decode($result, true);
return $result['result']['rate'] . " NPR";
}
add_shortcode('currency_conversion', 'currency_conversion_shortcode');
Related
my problem is that: Trying to retrieve the_content with a simple shortcode function, it retrieves only the title.
Even applying another filters the result is always the same.
The content is from a page.
The function is declared in the functions.php theme file.
Using the post (page) id.
function shtcode_Func( $atts = array() ) {
// set up default parameters
extract(shortcode_atts(array(
'id' => '5'
), $atts));
$my_postid = $atts;//This is page id or post id
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
add_shortcode('shortcodePage', 'shtcode_Func');
Calling from widget with [shortcodePage id=POST_ID] (int)
Result: Prints only the title.
I tried to change the filter with 'the_post_thumbnail' and retrieved the title again.
I'm desperated :(
Thanks!!
There are several things incorrect with your shortcode function, but the main things:
You are using extract but not using anything from extract
$atts is an array, not just the id.
You are using apply_filters('the_content'). This essentially overwrites WPs built in apply_filter. You want to use add_filter, but as you can see that won't be necessary.
Here is the shortcode trimmed down with what you are trying to do:
function shtcode_Func( $atts ) {
// set up default parameters. No need to use extract here.
$a = shortcode_atts(array(
'id' => ''
), $atts);
// Use get_the_content, and pass the actual ID
$content = get_the_content('','', $a['id'] );
// This is the same
$content = str_replace(']]>', ']]>', $content);
// Return the content.
return $content;
}
add_shortcode('shortcodePage', 'shtcode_Func');
Try to use like this:
function shtcode_Func( $atts = array() ) {
// set up default parameters
extract(shortcode_atts(array(
'id' => '5'
), $atts));
$content_post = get_post( $atts['id'] );
ob_start();
$content = $content_post->post_content;
$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]>', $content );
echo $content;
$str = ob_get_contents();
ob_end_clean();
return $str;
}
add_shortcode('shortcodePage', 'shtcode_Func');
So I am working with the coinmarketcap api and try to combinate it with WordPress.
In WordPress I am using the following php code:
function api() {
$url = 'https://api.coinmarketcap.com/v1/ticker/?start=0&limit=250';
$response = wp_remote_get( esc_url_raw( $url ) );
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
$name = $api_response[1]["name"];
$usd = $api_response[1]["price_usd"];
echo $name . "<br />";
echo $usd;
}
add_shortcode( 'api_short', 'api' );
So this code is working and I get my results on the WordPress page by using the shortcode api_short.
My problem now is that I want to use the shortcode on the following way:
[api short name ="20"]
This way I can switch from data easily by only using the shortcode instead of changing the code the whole time.
The 'name' variable in this case is the name of the cryptocurrency as can be seen here: https://api.coinmarketcap.com/v1/ticker/?start=0&limit=250 . 0 = bitcoin , 1 = ethereum.
I hope someone knows a way to get this working, I tried somethings but so far without a result.
The WordPress shortcode documentation can be found here: https://codex.wordpress.org/Shortcode_API
A few things:
First:
Prefix your functions - especially if you're using a name that's got a global scope or appeal.
api() should really be something along the lines of solaiman_api() to prevent any conflicts.
Second:
You should be using the WP Transients API to cache results so you don't rate limit the CMC API
Third:
For the actual answer to your question, you just need to parameterize the input value in the URL.
function solaiman_api( $atts ) {
extract( shortcode_atts( array(
'placeholder' => ''
), $atts ) );
$coin = $atts['coin'] ? $atts['coin'] : 0; // Default to 0 for BTC
$limit = $atts['limit'] ? $atts['limit'] : 250; // Default to 250
$url = 'https://api.coinmarketcap.com/v1/ticker/?start='. $coin .'&limit='. $limit;
$response = wp_remote_get( esc_url_raw( $url ) );
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
foreach( $api_response as $c ){
echo $c['name'].': $'.$c['price_usd'].'<br />';
}
}
add_shortcode( 'cmc_api', 'solaiman_api' );
Note I changed your function name and the shortcode, since api_short isn't terribly descriptive.
This shortcode would now look like this:
[cmc_api coin="1" limit="1"]
Which should give you just the price of Ethereum.
Use This Code Instead:
Here's a better one that caches the results for 10 minutes according to CMC's terms of service:
function solaiman_api( $atts ) {
extract( shortcode_atts( array(
'placeholder' => ''
), $atts ) );
$coin = $atts['coin'] ? $atts['coin'] : 0; // Default to 0 for BTC
$limit = $atts['limit'] ? $atts['limit'] : 250; // Default to 250
$url = 'https://api.coinmarketcap.com/v1/ticker/?start='. $coin .'&limit='. $limit;
$transient_name = 'cmc_api_'.$coin.'_'.$limit; // One transient per coin per limit
if( false === ( $response = get_transient( $transient_name ) ) ){
$response = wp_remote_get( esc_url_raw( $url ) );
set_transient( $transient_name, $response, 600 ); // Cache for 10 minutes
}
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
foreach( $api_response as $c ){
echo $c['name'].': $'.$c['price_usd'].'<br />';
}
}
add_shortcode( 'cmc_api', 'solaiman_api' );
You can see an example of [cmc_api coin="1" limit="1"] here: http://xhynk.com/headless/2018/02/28/cmc-test/
I have a function in the theme function file that pulls JSON data and creates a shortcode that I use in a page
My question is how to pass the RID=Value from a page using the shortcode
[json_employees] and send the RID back to the function?
function foobar2_func( $atts ){
ob_start();
$url = 'https://jdublu.com/api/wrsc/json_employee.php?RID=17965';
$data = file_get_contents($url);
$items = str_replace('<p></p>', '', $items);
$items = json_decode($data, true);
?>
add_shortcode( 'json_employees', 'foobar2_func' );
your shortcode should be
[json_employees RID='17965']
and shortcode function is
function foobar2_func( $atts ){
ob_start();
$atts = shortcode_atts(
array(
'RID' => 'id',
), $atts );
$url = 'https://jdublu.com/api/wrsc/json_employee.php?RID='.esc_attr($atts['RID']);
$data = file_get_contents($url);
$items = str_replace('<p></p>', '', $items);
$items = json_decode($data, true);
return $items;
}
add_shortcode( 'json_employees', 'foobar2_func' );
function foobar2_func( $atts ){
ob_start();
$atts = shortcode_atts(
array(
'RID' => 'id',
), $atts );
$url = 'https://jdublu.com/api/wrsc/json_employee.php?RID='.esc_attr($atts['RID']);
$data = file_get_contents($url);
$items = str_replace('<p></p>', '', $items);
$items = json_decode($data, true);
return $items;
print_r ($atts);
}
add_shortcode( 'json_employees', 'foobar2_func' );
and in the wordpress page
[json_employees RID='17965']
here is the page to test it http://bahiacr.com/test_json/
I wanna limit the result to 10 elements (10 words). This is the code I got:
function ls_bp_hashtags_cloud() {
$args = array () ;
if ( bp_is_activity_component() ) {
$toHead = __( 'Popular Hashtags across network' , 'bp-hashtags' ) ;
}
if ( bp_is_user_activity() ) {
$toHead = __( 'Hashtags by user' , 'bp-hashtags' ) ;
$args[ 'user_id' ] = bp_displayed_user_id() ;
}
if ( bp_is_group_activity() || bp_is_group_home() ) {
$toHead = __( 'Hashtags in group' , 'bp-hashtags' ) ;
$args[ 'if_activity_item_id' ] = bp_get_current_group_id() ;
}
echo '<div align="right"><h5>' . $toHead . '</h5>' ;
echo ls_bp_hashtags_generate_cloud( $args ) ;
echo '</div>' ;
}
And thisone below is the ls_bp_hashtags_generate_cloud() function:
function ls_bp_hashtags_generate_cloud( $args = array() ) {
$hashtags = ls_bp_hashtags_get_hashtags( $args );
$defaults = array(
'smallest' => 10, 'largest' => 10, 'unit' => 'pt', 'number' => 0,
'format' => 'flat', 'separator' => ",\n\n", 'orderby' => 'count', 'order' => 'DESC',
'topic_count_text_callback' => 'default_topic_count_text',
'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1
);
$args = wp_parse_args( $args, $defaults );
extract( $args );
$tag_cloud = wp_generate_tag_cloud( $hashtags, $args );
$tag_cloud = '<div class="hashtags">' . $tag_cloud . '</div>';
return $tag_cloud;
}
I got this other one too, not sure if it's needed:
function ls_bp_hashtags_get_hashtags( $args = array() ) {
global $wpdb;
$bp = buddypress();
$link = $bp->root_domain . "/" . $bp->activity->slug . "/" . BP_ACTIVITY_HASHTAGS_SLUG . "/";
bp_hashtags_set_constants();
$data = maybe_unserialize( get_site_option( 'ls_bp_hashtags' ) );
if ( $data['style']['show_hashsymbol'] == '1' ) {
$hashtag_name = ' CONCAT( "#", hashtag_name)';
} else {
$hashtag_name = 'hashtag_name ';
}
$toWhere = ls_bp_hashtags_generate_query_limitations( $args );
$results = $wpdb->get_results( 'SELECT COUNT(hashtag_name) as count, '
. $hashtag_name . ' as name, '
. 'CONCAT("' . $link . '", hashtag_slug) as link
FROM ' . BP_HASHTAGS_TABLE . ' WHERE 1=1 ' . $toWhere . ' GROUP BY hashtag_name' );
return $results;
}
The first snippet I pasted here is in one file while the last 2 snippets are in another file.
I think I have to modify the function ls_bp_hashtags_generate_cloud(), the point is I don't know how to do that. I tried with array_slice() and with $sql = "SELECT * FROM bp_activity LIMIT 10"; I checked mysql database, my ashtags are located in bp_ashtags table, but when I open this table I see each hashtag is referred to be in bp_activity table.
This function gives me the ashtags cloud of my buddypress activity stream page, so it gives me all the ahstags present in the database, instead I wanna limit the result to just 10 #ashtags (it's a wordpress website). That said I'm really a novice so please if you can help me giving me the whole snippet to add and where to put it, I know almost nothing about php coding. If you need to know other things about this code, just tell me; thisone is an extract so I don't know if I'm missing something for the solution. Thank you in advance
Yes, I have to modify the function ls_bp_hashtags_generate_cloud(), the point is I don't know how to do that. I tried with array_slice() and with $sql = "SELECT * FROM bp_activity LIMIT 10"; I checked mysql database, my ashtags are located in bp_ashtags table, but when I open this table I see each hashtag is referred to be in bp_activity table. This function gives me the ashtags cloud of my buddypress activity stream page, so it gives me all the ahstags present in the database, instead I wanna limit the result to just 10 #ashtags (it's a wordpress website). That said I'm really a novice so please if you can help me giving me the whole snippet to add and where to put it.
I am working on plugin which will work automatically via wordpress schedule hooks. But to do that I need a single record from database and process it and next schedule run, I want to get next single record from database to process.
I am using custom table for my data, the table have a primary key with auto increment value.
So how can I get next record every time when wordpress schedule hook runs ?
I have tried with sessions/php pagination and Its working outside of wordpress but wordpress does not provide session support so I can't use it. I can use $wpdb global class for single record retrieval but I don't have a idea about next record retrieval.
I hope I have explained my problem clearly.
here is the function which will execute on wp schedule hook:
function ACP_poster($n){
global $wpdb,$api_key;
$row = $wpdb->get_results('SELECT a,b,c FROM tbname ORDER BY id DESC LIMIT 1',ARRAY_A);
$url = 'https://example.com';
$headers = array( 'Authorization' => $api_key );
$request = new WP_Http;
$result = $request->request( $url , array( 'method' => 'GET', 'headers' => $headers, 'sslverify' => false ) );
$data = new SimpleXMLElement($result['body']);
$attributes = $data->p->attributes();
foreach ($data->p[0] as $p)
{
// Sanitize data.
$pr = number_format((float)$p->pr, 2, '.', ' ');
$image = '<img src="'.$p->{'url3'} .'" style="float: right"/>';
$pd = $image.$p->description .'-- ='.$p->currency.' '.
' ,Retail Price ='.$p->{'retail-price'}.' , '.
$p->{'sale-price'}.'<a href="'.$p->{'url2'}.
'">...</a>';
$v = $c[$no]->child;
$ids = get_term_by('slug', $v, 'category');//wordpress function
$id = (int)$ids->term_taxonomy_id;
$p2 = array('post_title' => $p->name,
'post_content' => $pd,
'post_status' => 'publish',
'post_author' => 1,
'post_category' =>array($id));
$pr = wp_insert_post( $p2, $wp_error );
wp_reset_query(); // Restore global post data stomped by the_post().
}
}
}