WordPress - Display Random Number Between 190-250 - php

Does anyone know of a way to display a random number between 190-250 inside a specific page?
For example:
Today I had Display Random Number Coffees.

The way I would accomplish this is with a shortcode, like this:
function na_random_number_shortcode( $atts ) {
global $post;
$args = shortcode_atts(
array(
'min' => 190,
'max' => 250,
'id' => $post->ID,
'hours' => 24
),
$atts, 'random_number'
);
// Use transient to store the random number temporarily
if ( false === ( $random_number = get_transient( 'random_number_'.$args['id'] ) ) ) {
$random_number = mt_rand( (int) $args['min'], (int) $args['max'] );
set_transient( 'random_number_'.$args['id'], $random_number, HOUR_IN_SECONDS * $args['hours'] );
}
return $random_number;
}
add_shortcode( 'random_number', 'na_random_number_shortcode' );
Putting that code in your theme's functions.php file would allow you to enter
"Today I had [random_number] Coffees."
and display a random number between 190 and 250.
It's also flexible; you can do something like [random_number min="1" max="10"] to get a random number between 1 and 10.
By default, it will remember the random number for 24 hours. You can change this by passing in the "hours" attribute, like: [random_number hours="12"]
If you have more than one of these on a page, and you want the numbers to be different, you have to give each one a unique id. So if you had two of them, you could do: [random_number id="1"] and [random_number id="2"]

You can also use the PHP rand function in the page where you want it:
echo(rand(190,250));

Related

How to arrange columns in my Admin bar in WordPress dashboard?

I am using the below code in my functions.php to show a 60x60 image thumbnail of each posts featured image on WordPress dashboard but it shows up in like the 3rd column and I would like it to show up first to the left of the title column - can't seem to make that work - any suggestions anyone?
// show featured images in dashboard
add_image_size( 'showimg-admin-post-featured-image', 60, 60, false );
// Add the posts and pages columns filter. both use the same function.
add_filter('manage_posts_columns', 'showimg_add_post_admin_thumbnail_column', 2);
add_filter('manage_pages_columns', 'showimg_add_post_admin_thumbnail_column', 2);
// Add the featured image column
function showimg_add_post_admin_thumbnail_column($showimg_columns){
$showimg_columns['showimg_thumb'] = __('Featured Image');
return $showimg_columns;
}
// Manage Post and Page Admin Panel Columns
add_action('manage_posts_custom_column', 'showimg_show_post_thumbnail_column', 5, 2);
add_action('manage_pages_custom_column', 'showimg_show_post_thumbnail_column', 5, 2);
// Get featured-thumbnail size post thumbnail and display it
function showimg_show_post_thumbnail_column($showimg_columns, $showimg_id){
switch($showimg_columns){
case 'showimg_thumb':
if( function_exists('the_post_thumbnail') ) {
echo the_post_thumbnail( 'showimg-admin-post-featured-image' );
}
else
echo 'hmm… your theme doesn\'t support featured image…';
break;
}
}
To insert the item to the right of the checkbox (index 0) in the array.
Use array_merge and array_slice to add to the column array.
// Add the featured image column
function showimg_add_post_admin_thumbnail_column($showimg_columns){
$new_column['showimg_thumb'] = __('Featured Image');
return array_merge(
array_slice( $showimg_columns, 0, 1, true ),
$new_column,
array_slice( $showimg_columns, 1, null, true )
);
}
For reference... the default Column Order Array is this:
array(
'cb' => '<input type="checkbox" />',
'title' => "Title",
'author' => "Author",
'categories' => "Categories",
'tags' => "Tags",
'comments' => '<span class="vers comment-grey-bubble" title="Comments"><span class="screen-reader-text">Comments</span></span>',
'date' => "Date"
);

How to prevent duplicate entries in Gravity Forms within specific dates

I am trying to prevent duplicate entries for 2 of my Gravity Forms. I need to prevent these entries only within a certain timeframe and based on certain fields like 'email' or 'address'. So if they have an entry with the same 'email' prior to the start_date, they can still submit the form without issue. If they have an entry with the same 'email' after the start_date and before the end_date then they won't be able to submit the form. The code below is what I currently have in place, but it isn't working. Any help is greatly appreciated.
add_filter( 'gform_is_duplicate', 'custom_duplicate_entry_rules', 10, 4 );
function custom_duplicate_entry_rules( $count, $form_id, $field, $value ) {
$form_id = array( 6,61 );
$search_criteria = array(
'start_date' => 2021-02-01, // Get entries from date after last event
'end_date' => 2022-01-20, // Up to right before next event
'field_filters' => array( // which fields to search
array(
'key' => 'email',
)
)
);
$search_past_entries = GFAPI::count_entries($form_id, $search_criteria);
if ( $search_past_entries >= '1' ) {
echo 'You have already signed up.';
}
}
I managed to get this figured out with the help of a fellow developer...
add_filter( 'gform_is_duplicate_6', 'custom_duplicate_entry_rules', 10, 4 );
add_filter( 'gform_is_duplicate_61', 'custom_duplicate_entry_rules', 10, 4 );
function custom_duplicate_entry_rules( $count, $form_id, $field, $value ) {
$search_criteria = array(
'start_date' => '2021-02-01', // Get entries from date after last event
'end_date' => '2022-01-20', // Up to right before next event
'field_filters' => array( // which fields to search
array(
'key' => 2,
'value' => $value,
)
)
);
$count = GFAPI::count_entries( $form_id, $search_criteria );
if( $count >= 1 ) {
return $count;
} else {
return 0;
}
}
I needed to zero in on the specific entry meta in field_filters which in this case ended up being 2, for the email address (this is the field ID in Gravity Forms). Lastly, I changed the if statement to return $count or 0. Gravity Forms does all the heavy lifting beyond that.

WordPress posts orderby meta value with strings and numbers

I have a custom field (advanced custom field) called custom_order and I want WordPress to sort all posts by the field values.
The custom_order field's values can be written in 3 ways:
numbers only (3, 400, 6424)
numbers, then strings (3A, 3B, 5000A)
numbers, then strings, then numbers (3A1, 3A2, 401AZ1, 9000A3, 9000A1)
More examples of custom_order values:
Wanted order:
1, 2, 3, 3A, 3B, 3C, 3CA, 4, 400, 401, 401A, 401AZ, 401Z, 9000, 9000A, 9000A1, 9000A3
Conditions:
3 should come before 200 (numeric order)
3 should come before 3A (number and strings)
3A should come before 3A1 (numbers, strings, numbers)
My try:
My current code is sorting by numbers (condition 1), but ignores condition 2 and condition 3.
Therefore, my code gives me this order:
1, 2, 3A, 3, 3C, 3B, 4 <-- all 3's are not ordered, which is bad
The code I'm using (in functions.php):
add_action('pre_get_posts', function ($q) {
if (
!is_admin() // Target only front end queries
&& $q->is_main_query() // Target the main query only
&& ($q->is_search() || $q->is_post_type_archive('data-base'))
) {
$q->set('meta_key', 'custom_order');
$q->set('order', 'DESC');
$q->set('orderby', 'meta_value_num');
}
});
I found a neat solution!
$q->set(
'meta_query',
array(
'relation' => 'OR', //**** Use AND or OR as per your required Where Clause
'clause1' => array(
'key' => 'custom_order',
'compare' => 'EXISTS',
),
)
);
$q->set(
'orderby',
array(
'meta_value_num' => 'ASC', // this reorders by numbers
'meta_value' => 'ASC', // this reorders by strings after sorting numbers
)
);

Using supplier with largest margin using PHP logic

I have the following values from a database call that I want to apply some logic to. I thought I could originally use PHP's max however this doesn't appear to be the case.
I have three suppliers of a product. They might not all stock the item I am displaying, and they all offer a different margin, on a product by product basis though, so that is why I can't just say generally supplier 1 is better than supplier 2 etc.
$supplier1Live = 1
$supplier2Live = 1
$supplier3Live = 0
$marginSupplier1 = 20
$marginSupplier2 = 40
$martinSupplier3 = 50
In this example I would want to use Supplier 2 as they stock the product supplier2Live = 1 and also have the better margin than the other supplier who stocks the product (supplier1)
My mind however is drawing a complete blank in how to code this?
I thought I could add it to an array giving:
$array = array(
"supplier1" => array(
"live" => 1,
"margin" => 20
),
"supplier2" => array(
"live" => 1,
"margin" => 40
),
"supplier3" => array(
"live" => 0,
"margin" => 50
)
);
And run something on that, but not sure what to.
Filter the array using array_filter (filter by live==1), and then find the maximum out of the resultant array (maximum on the "margin" value)
Like this, if I understand correctly
$array = array(
"supplier1" => array(
"live" => 1,
"margin" => 20
),
"supplier2" => array(
"live" => 1,
"margin" => 40
),
"supplier3" => array(
"live" => 0,
"margin" => 50
)
);
$res = array_filter($array,function($v){return $v["live"];});
$supplier = array_reduce($res, function($a, $b){
return $a["margin"]>$b["margin"]?$a:$b;
});
print_r($supplier);
Try something like this:
$best_supplier = null;
$best_supplier_margin = null;
foreach($array as $name => $supplier) {
if($supplier['live']) {
if($supplier['margin'] > $best_supplier_margin || is_null($best_supplier_margin)) {
$best_supplier = $name;
$best_supplier_margin = $supplier['margin'];
}
}
}
if(is_null($best_supplier)) throw new Exception('No suppliers are live!');
echo $best_supplier;
So you basically want to find the max of supplierXLive * marginSupplierX?
You can also implement a custom compare function and provide it to PHPs usort() function

banner rotator advertising with probability

I have banners advertising with number of views, like CPM system.
And for example :
i have 3 banner:
banner1 with 20.000 nr of views
banner2 with 10.000 nr of views
banner3 with 5.000 nr of views
and on my website the banner must to appear in this position (when the page is reloaded) :
banner1 banner2 banner1 banner2 banner3
if the number of views is higher then the probability of apparition is higher
how can i do this in php?
First of all, your system is just... stupid. It perpetuates banners with lots of views while newly created banners with 0 or few views will never get a chance to be picked and thus will never be actually seen...
That being said, if you have an array that looks like this:
$banners = array
(
'banner1' => 1,
'banner2' => 2,
'banner3' => 4,
'banner4' => 8,
'banner5' => 16,
);
You can use a function like this one to weightily pick one banner:
function Probability($data)
{
if (is_array($data) === true) {
$result = 0;
$probability = mt_rand(1, array_sum($data));
foreach ($data as $key => $value) {
$result += $value;
if ($result >= $probability) {
return $key;
}
}
}
return false;
}
Usage (test it # CodePad.org or # IDEOne):
echo Probability($banners); // banner5
Sample from 100 executions:
Array
(
[banner5] => 41
[banner4] => 38
[banner3] => 10
[banner2] => 8
[banner1] => 3
)
Here's a php way to do it
I'm imagining your array will look something like this...
$banners = array(
array (
'name' => 'banner1',
'views' => 20
),
array (
'name' => 'banner2',
'views' => 10
),
array (
'name' => 'banner3',
'views' => 5
)
);
This function basically loops through the banners and however many views a banner has, that many items of its array index are added to an array. Then a random one is chosen. Items with more views have a better chance of being chosen.
function getWeightedRandom( $array ) {
$universe_array = array();
foreach ( $array as $k => $b ) {
$universe += $b['views'];
$universe_array = array_pad( $universe_array, $universe, $k );
}
$rand = mt_rand( 0, count( $universe_array ) -1 );
return $array[ $universe_array[ $rand ] ];
}
$r = getWeightedRandom($banners);
print_r($r);
A simple mysql option is:
select * from banners order by rand() * views desc limit 1
banners with more views will have a higher chance of being the top result

Categories