I have a widget that is saving an option perfectly - the problem lies when I want to get the option value on the front end and then use that value to query the posts. How can I get that value from the database? I've tried using get_option and because the way the value is stored the array position changes each time.
My code is simply:
$cjd_length = get_option( 'widget_cjd_list_widget' );
if( !empty( $cjd_length[3]['length'] ) ) {
$length = $cjd_length[3]['length'];
}
else {
$length = 3;
}
In the method WP_Widget::widget($args, $settings) you get the Widget Settings. From outside you must know the widget-id. You get it in the update Method ($_REQUEST['widget-id'])
Related
I'm trying to show the ACF repeater image in the dynamic slider in oxygen via the PHP function from the specified page id.
ACF field: slajder
Subfield with img repeater: obraz_slajdera
Page id: 7219
I always get background-img unknown.
My code:
function get_slider() {
$image = get_field( 'img', 7219 )['sizes']['large'];
return $image;
}
Please help.
You may want to do some reading through the ACF developer documentation, your code is fragmented, and the get_field() function isn't being used properly. At any rate, I'll do my best to explain what you should be directing your solution towards. You'll first need to loop through your repeater fields, check to see if there's any "rows" in the repeater field, fetch your value, and then do whatever you need to do with your respective image.
So, technically your function should look something like this:
function fetchSliderImages() {
//Empty array for holding your slider images, assuming there's more than one
$sliderImages = array();
//Check to see if the slider even has rows
if( have_rows('slajder') ):
//Loop through all the rows of your slider
while( have_rows('slajder') ) : the_row();
//Assuming this is coming back as a URL (can be adjusted within ACF)
$imageRepeaterValue = get_sub_field('obraz_slajdera');
//Check to see that we actually got something back that isn't blank
if($imageRepeaterValue != '') {
array_push($sliderImages, $imageRepeaterValue);
}
endwhile;
endif;
return $sliderImages;
}
You can then use this function to return an array of URL's which are slider images.
Used this for reference: https://www.advancedcustomfields.com/resources/repeater/
I have created a repeatable field with CMB2, and created a normal field. This is the function of https://pastebin.com/XUQgkvbi
If you use foreach for repeatable using a post or page then you can show data as: https://pastebin.com/C35vWGDs
And Call normal field without repeatable, then
<?php $ entries = get_post_meta (get_the_ID (), 'yourprefix_group_demo', true); ?>
<?php echo $ entries; ?>
also work.
But the problem is, I do not want to use the above function on any page or post. I want to use it in the Options Page. The above Function option has been added to the option page, but I can not do the data show of those files in any way.
I've tried get_post_meta () and get_option () with two functions, but in no way can I show data from the option page. How can I get the data from the above fields (option page) to the show in the frontend? Please help with a little bit.
I got solution, The options are stored in a single option field. You would loop through the news-section groups with something like this:
$settings = get_option( 'repeatable-news-options.php', array() );
if ( ! empty( $settings['news-section'] ) ) {
foreach ( $settings['news-section'] as $section ) {
echo $section['title'] . '<br/>';
}
}
that link https://wordpress.org/support/topic/how-to-display-data-from-cmb2-option-page/
problem solved.
I've search the interwebs high and low looking for a way to conditionally display WooCommerce product attribute data on certain pages.
Ex: if attribute somethingcool has a value and it's on /?filtering=1&filter_manufacturer=1648 page, display value
I'd like to display an attribute differently if they are on a specific filtered page
Ex:
http://www.colorselectcoil.com/color-match-tool/?filtering=1&filter_manufacturer=1648
Based on this filtered page, display a product attribute 'somethingcool'
<?php if ( is_product_attribute('somethingcool') ) {
echo 'Hi! This is something cool?';}
else {
echo '';
}
?>
If it were a normal WordPress page, not a filtered page I could hide and show based on body class tag but unfortunately the body class doesn't change based on query string urls.
You could use the url search string and extract from it whatever part you wanted. For example, if you only wanted to know if you were on a filtered page then you wouldn't bother checking for the manufacturer.
<?php if ( $product->get_attribute('Certainteed') && intval($_GET['filtered']) == 1 && intval($_GET['filter_manufacturer']) == 1648 ) {
echo 'Hi! This is something cool?';
}
?>
The intval() bit should be enough to prevent injects.
If you wanted to find a part of the uri you could use something like:
if( stristr( htmlspecialchars($_SERVER['REQUEST_URI']), 'color-match-tool') && is_product_attribute('somethingcool') ){
echo 'Hi! This is something cool!';
}
Check to see what echo is_product_attribute('Everlast'); as per your example returns.
$_GET... gets the variables in the search string by their names, which go in the square brackets.
<?php if( stristr( htmlspecialchars($_SERVER['REQUEST_URI']), 'color-match-tool') && intval($_GET['filtering']) == 1 && intval($_GET['filter_manufacturer']) == 1694 && is_product_attribute('Everlast') ){
echo 'Hi! This is something cool!';
} ?>
Try it with just one item at a time and build up to get the hang of it.
Loads about finding strings in strings How do I check if a string contains a specific word in PHP?
Other ways to get and echo a product attribute if need be:
Woocommerce getting custom attributes
I am not sure if this would work, but it might be adaptable enough.
You could pass the filter value $ff the filter integer value you want $ffv (might be different from 1) $fm is the $filter_manufacturer integer value $fmv - is the value you are looking for it to be - is as your example 1648 then the product array $product, your $collection variable and $aiw is the "Attribute I want" in text form but you might also pass it in a variable.
Put this function in your functions.php
function attribute_i_want( $ff,$ffv, $fm, $fmv, $prod, $coll, $aiw){
if( $ff == 1 && $fm == $fmv && $collection = $prod->get_attribute($aiw) ){
echo '<div class="attribute-titles">. $aiw . ' name: ';
echo ($coll = $prod->get_attribute($aiw) ) ? $collection : __('', 'woocommerce'); echo '</div>';
}
}
add_action( 'wp_enqueue_scripts', 'attribute_i_want' );
And call it with this little lot passed to it
<?php attribute_i_want( intval($_GET['filtering']), 1, intval($_GET['filter_manufacturer']), 1648, $product, $coll, 'Certainteed'); ?>
I have assumed that the code you posted is working as it stands.
Looking at your page I can't see how it is getting the value from the dropdown list as it has no name or ID - ideally in the onchange event onchange="setManufacturer()"; - which I also can't find but believe it to be an event listner script - you would use something like this to set a hidden variable which gets the dropdown text value and uses that for the slot in the function call where you would currently have to enter the text manually, like 'Certainteed' in the example:
<script language=JavaScript>
function setManufacturer(){
var manufacturerName = manufacturerDropdown.options[manufacturerName.selectedIndex].innerHTML;
documentGetElementById('submittedManufacturerName').value = manufacturerName;
}
</script>
This would get the text of what is selected from the select dropdown - which would need the ID "manufacturerName" and insert that text value into the hidden input's value, which PHP would be able to pick up and use in the PHP function call.
The onchange event would trigger the JavaScript and submit the page, the php would pick up the text value from the hidden input and that is what would be put into the function call:
<input type="hidden" id ="submittedManufacturerName" name="submittedManufacturerName" value = "" />
<?php attribute_i_want( intval($_GET['filtering']), 1, intval($_GET['filter_manufacturer']), $manufacturerName, $product, $coll, $submittedManufacturerName); ?>
Unless there is any other way you can get at the value for the manufacturer name in a PHP variable - which may be possible as the manufacturer name does appear elsewhere on the page in the link above your dropdown, so it may already exist as a PHP variable which you could use as it is. That way your function would be fully automatic without the need for this additional JavaScript. The value appears in the a href called "Remove filter" on your existing page.
$manufacturerName would be the submitted value from the dropdown's actual value collected as $manufacturerName = htmlspecialchars($_GET['manufacturerDropdown']);
Steve hooked it up and helped me figure out how to write conditional based on filter, which fixed my issue. Thanks Steve!
<?php if( intval($_GET['filtering']) == 1 && intval($_GET['filter_manufacturer']) == 1648 && $collection == $product->get_attribute('Certainteed') ){ echo '<div class="attribute-titles">Certainteed name: '; echo ($collection == $product->get_attribute('Certainteed') ) ? $collection : __('', 'woocommerce'); echo '</div>'; } ?>
I don't know if this is the correct place to ask, but I'll try. I have an ongoing project with Wordpress (Genesis framework - Agentpress PRO theme) working with Ninja Forms, and am having a slight problem. The web page is: http://www.supercastor.net/wordpress/listings/castellon-65-m2-ytong/
I have a listings page (for each house model). Each listing has metadata that is accessible via a simple function from the genesis framework:
genesis_get_custom_field( 'CUSTOM_FIELD_NAME' );
I would like to have, like the link above has, a list of checkboxes that the user can select/deselect, and the TOTAL value is finally calculated. Everything is working, except that I cannot assign the value of this "custom field" that is from the listing.
I am using Ninja Forms, and after some research, I have come up with the solution of registering a custom field in NF:
function precio_basico_calc_display( $field_id, $data ){
// Get the default_value
if( isset( $data['default_value'] ) ){
$def_value_new = genesis_get_custom_field( '_listing_precio_sort' );
$data['default_value'] = $def_value_new;
$default_value = $data['default_value'];
debug_to_console("Default_value is set to:$default_value");
}else{
$default_value = '';
debug_to_console("Default_value NOT SET");
}
$products_select = genesis_get_custom_field( '_listing_precio_sort' );
}
// Now that $products_select is populated with the listing_price, output a checkbox with the value of the price. ?>
<input type="checkbox" name="ninja_forms_field_<?php echo $field_id;?>" value="<?php echo $products_select;?>" checked="checked" class="ninja-forms-field ninja-forms-field-calc-listen" disabled="disabled">
<?php
}
I also have a small "debug_to_console" function, but this is just as a quick "debugging". I can see that the value is there, but in the calculation it is not set. I have set the default to 40.000, but it should be set to the value for each price (in the link example, should be 19.000). As:
$products_select = genesis_get_custom_field( '_listing_precio_sort' );
And I have also tried the following (field_id 33 is the checkbox in the form):
add_filter( 'ninja_forms_field', 'my_filter_function', 10, 2 );
function my_filter_function( $data, $field_id ){
if( $field_id == 33 ){
$listing_price = genesis_get_custom_field( '_listing_precio_sort' );
$data['default_value'] = $listing_price;
}
return $data;
}
But it isn't working either. I mean, it does display the value (in the console), but not making the calculation correctly.
Help! I just want the calculation (TOTAL) to update correctly for each listing. I thought changing the default_value would do, but it doesn't. I'm lost :(
Thanks!
Hi all, thanks for reading.
Environment :
Wordpress + Advanced Custom Fields plugin
Problem :
I have searched for hours now and I can't seem to find the correct syntax to do the following:
When posting a new post, get custom field value in order to automatically replace the title of the post by that value. Example: I create a post and set '10am' in my 'time' custom field. The title of the post is automatically replaced by '10am'.
Example:
So I'm adding a filter with the following :
add_filter('wp_insert_post_data', 'change_title') ;
function change_title($data)
{
$time = XXX ; // How should I get this custom field value ?
$new_title = 'Topic created at'.$time ;
$data['post_title'] = $time ;
return $data;
}
It must be very simple but I have tried every function available on both WP and the plugin's documentations. I would be very thankful if anyone passing by gave me the solution.
Thanks in advance !
Tweak to Riadh's accepted answer (would add as a comment but haven't got enough rep yet):
As documented in the WordPress Codex wp_update_post includes the save_post hook so calling wp_update_post() inside the save_post hook creates an infinite loop. To avoid this, unhook then rehook your function like so:
add_action('save_post', 'change_title');
function change_title($post_id) {
$time = get_field('time',$post_id);
$post_title = 'Topic created at '. $time;
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'change_title');
// update the post, which calls save_post again
wp_update_post(array('ID' => $post_id, 'post_title' => $post_title));
// re-hook this function
add_action('save_post', 'change_title');
}
You can actually access the global $_POST variable for your field value , but i guess you can do it in a cleaner way by using the save_post action to update your post's title, eg:
add_action('save_post', 'change_title');
function change_title($post_id) {
$time = get_field('time',$post_id);
$post_title = 'Topic created at '. $time;
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'change_title');
// update the post, which calls save_post again
wp_update_post(array('ID' => $post_id, 'post_title' => $post_title));
// re-hook this function
add_action('save_post', 'change_title');
}
assuming that your ACF fieldname is "time".
Edit: Updated the answer as per Mark Chitty's answer.
You may try this
add_filter( 'wp_insert_post_data', 'change_title', '99', 2 );
function change_title($data , $postarr){
$custom_field = 'custom_filed_name';
$post_id = $postarr['ID'];
$time = get_post_meta( $post_id, $custom_field, true );
// Now you have the value, do whatever you want
}
Advanced Custom Fields creates a 'field key' for each custom field that is created. I was able to refer to this key value when trying to access the custom fields. The field key value can be found by viewing page source when viewing the post type within the Wordpress admin section.
Look for data-field-key. You will see a value similar to data-field-key="field_5847b00820f13" in the page source. Use this value when accessing the value in the $postarr argument in the wp_insert_post_data filter. The custom fields will be in a nested array named fields within the $postarr argument.
Alternatively, the field key value can be located by navigating to the Advanced Custom Fields / Export option from within the admin section. Once you are on the export page for Advanced Custom Fields, select the export to PHP option and you will see the value in the resulting PHP code.
In the example below, I am concatenating two Advanced Custom Fields and updating the post_title in the $data array returned from the function.
The result is that the post_title value will be saved to the database via the built in Wordpress save post logic.
add_filter('wp_insert_post_data', 'slb_set_title', '99', 2);
function slb_set_title ($data, $postarr){
if($data['post_type']==='slb_subscriber'){
$data['post_title'] = $postarr['fields']['field_5847b00820f13'] .' '.
$postarr['fields']['field_5847b03f20f14'];
}
return $data;
}