How to display data from cmb2 option page? - php

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.

Related

Show images in slider from acf repeater

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/

How can I add a meta field value to WP footer (inline)?

I have meta fields containg json schema mark up for products. I want to add these to the footer. I have tried using this function
add_action('wp_footer', 'insert_my_page_schema');
function insert_my_page_schema() {
if ( ! is_product() ) {
return;
}
$schema = get_post_meta( get_the_ID(), 'schema', true);
if ( ! $schema ) {
return;
}
echo $schema;
}
but this adds the code into the frontend, visible for the viewer of the site. How can I add mycustom field values inline to the footer, so that it is only visible for Google/in the source code, but not in frontend?
It can be issue with wrapping schema code with <script type="application/ld+json"></script> tag, if you want to add schema then must need to wrap script tag as per this google reference example - https://developers.google.com/search/docs/advanced/structured-data/intro-structured-data
You may achieve this using anyone of below option
Wrap <script type="application/ld+json">{your schema code here}</script> in scheme field value from backend
You can wrap script tag in code itself echo '<script type="application/ld+json">'. $schema .'</script>'; instead of echo $schema;

Using woocommerce, need to use a specific field from the database, how?

I found where in my database the information is that I need to echo out, but I don't know how to echo it out!
It's in the wp_postmeta > web_detail field - see screenshot: https://i.imgur.com/l9AXrX5.png
The data I need to show on the product page is the bit where it currently says: "TJOBBE"
I need this on my content-single-product.php - for every product where this information exists.
You can use get_post_meta() function that retrieves custom fields - https://developer.wordpress.org/reference/functions/get_post_meta/. In your case something like this:
$web_detail = get_post_meta( get_the_ID(), 'web_detail' );
if ( !empty( $web_detail ) ) {
//do smth with this data
}

How do you write conditional PHP based on WooCommerce attribute

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>'; } ?>

WordPress get widget option value

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'])

Categories