Fetching revolution slider shortcodes - php

I'm trying something new here. I have a Wordpress custom theme. I have Advanced custom fields all setup. I'm using the advanced custom field's load_field function on a "select" field with "choices". What I want to basically do is to show the revolution slider shortcodes as choices for this field... This is my code in the functions.php file. Any help would be highly appreciated! :)
<?php
function my_acf_load_field( $field ) {
$field['choices'] = array(
<-- WANT REVOLUTION SLIDER SHORTCODES HERE -->
);
return $field;
}
// all
// add_filter('acf/load_field', 'my_acf_load_field');
// type
add_filter('acf/load_field/type=select', 'my_acf_load_field');
// name
// add_filter('acf/load_field/name=my_select', 'my_acf_load_field');
// key
// add_filter('acf/load_field/key=field_508a263b40457', 'my_acf_load_field');
?>

Answer updated April 22, 2020: For Slider Revolution V6, the function getAllSliderAliases() has been replaced with get_sliders() and returns an array of objects instead of an array of strings. Source.
functions.php for Slider Revolution V6
function my_acf_load_field( $field ) {
if ( class_exists( 'RevSlider' ) ) {
$rev_slider = new RevSlider();
$sliders = $rev_slider->get_sliders();
if(count($sliders) > 0) {
foreach($sliders as $slider)
{
$field['choices'][$slider->alias] = $slider->title;
}
} else {
$field['choices'] = array( 'none' => 'No sliders exist. You must create one first.' );
}
} else {
$field['choices'] = array( 'none' => 'Slider Revolution plugin was not found.' );
}
return $field;
}
//Only place these options on a specific select field with the alias "rev_slider"
add_filter('acf/load_field/name=rev_slider', 'my_acf_load_field');
And then on the template or whatever page you're using the custom field on, I placed the actual shortcode there instead.
page.php
$slider_alias = get_field('rev_slider');
if(!empty($slider_alias)) {
echo do_shortcode(sprintf('[rev_slider alias="%s"]', $slider_alias));
}
Old Answer for versions of Slider Revolution before V6
For whatever reason, my $sliders variable from Andrew M's answer wasn't returning anything, so I came up with this solution instead based on Themepunch's documentation for displaying any slider at random and this article for checking if the class exists first to avoid errors.
functions.php for Slider Revolution versions before V6
function my_acf_load_field( $field ) {
if ( class_exists( 'RevSlider' ) ) {
$rev_slider = new RevSlider();
$slider_aliases = $rev_slider->getAllSliderAliases();
if(count($slider_aliases) > 0) {
foreach($slider_aliases as $slider_alias)
{
$field['choices'][$slider_alias] = $slider_alias;
}
} else {
$field['choices'] = array( 'none' => 'No sliders exist. You must create one first.' );
}
} else {
$field['choices'] = array( 'none' => 'Slider Revolution plugin was not found.' );
}
return $field;
}
//Only place these options on a specific select field with the alias "rev_slider"
add_filter('acf/load_field/name=rev_slider', 'my_acf_load_field');

What you could do is the following. Revolution slider slider items are stored in a table called wp_revslider_sliders (the wp_ part may change based on how you set up the database - check the table name first)
You can query this table using the Wordpress $wpdb global and get back the alias field - which is used as the shortcode. So in the body of your load field function you could try something like this
function my_acf_load_field( $field ) {
global $wpdb;
$query = sprintf('select r.id, r.alias from %srevslider_sliders r',$wpdb->prefix);
$sliders = $wpdb->get_results($query,OBJECT);
foreach($sliders as $slider)
{
//This just formats the string with the correct short code
$field['choices'][$slider->alias] = sprintf('[rev_slider alias="%s"]',$slider->alias);
}
return $field;
}
That should populate your dropdown with the right options - or at least get you on the right path

From Slider Revolution V6 the method "getAllSliderAliases" is not available anymore.
New Code is :
functions.php
function my_acf_load_field($field)
{
if (class_exists('RevSlider')) {
$rev_slider = new RevSlider();
$slider_aliases = $rev_slider->get_sliders();
if (count($slider_aliases) > 0) {
foreach ($slider_aliases as $slider_alias) {
$field['choices'][$slider_alias->alias] = $slider_alias->alias;
}
} else {
$field['choices'] = array('none' => 'No sliders exist. You must create one first.');
}
} else {
$field['choices'] = array('none' => 'Slider Revolution plugin was not found.');
}
return $field;
}
//Only place these options on a specific select field with the alias "rev_slider"
add_filter('acf/load_field/name=rev_slider', 'my_acf_load_field');
page.php
$slider_alias = get_field('rev_slider');
if(!empty($slider_alias)) {
echo do_shortcode(sprintf('[rev_slider alias="%s"]', $slider_alias));
}

Related

woocommerce_update_product does not get vendor terms on save

I'm using woocommerce_update_product hook to get product data once a product has been created / updated.
I'm trying to get the terms from a taxonomy called wcpv_product_vendors. On first save (as draft) I can get the other product_cat terms but the terms for wcpv_product_vendors don't appear until I next save the post.
add_action( 'woocommerce_update_product', [$this, 'dcgsql_update_product'], 50, 1);
public function dcgsql_update_product(){
$get_vendors = get_the_terms($product_id, 'wcpv_product_vendors');
if (!empty($get_vendors)) {
foreach ($get_vendors as $vendor) {
$vendorsarray[] = $vendor->term_id;
}
$vendors = implode(',',$vendorsarray);
} else {
$vendors = '';
}
}
On first save as draft, $get_vendors is always empty but I have applied one.
All other terms for product_cat are coming back fine.
Use the below codes it will work...
add_action( 'woocommerce_update_product', 'dcgsql_update_product', 50, 1);
public function dcgsql_update_product(){
$get_vendors = wp_get_post_terms($product_id, 'wcpv_product_vendors');
if (!empty($get_vendors)) {
foreach ($get_vendors as $vendor) {
$vendorsarray[] = $vendor->term_id;
}
$vendors = implode(',',$vendorsarray);
} else {
$vendors = '';
}
}

If statement for True/False user field wont work

I have created a ACF True/False field which is attached to my users. This is used as a access level option, so if the option is True they will see a link in the menu, if false, they will see a fallback link.
Below is the code iv added to my functions.php where iv created a shortcode: “customLink” which is then added to my menu link.
Below is what i have so far, which hopefully shows what im trying to do. If the users role us ‘my_custom_role’, then to get the ACF value, and if true, show one link, and if false show another.
add_shortcode('customLink', 'cm_link');
function cm_link($atts) {
$atts = shortcode_atts(array('title' => 'Custom Link', 'fallbackurl' => '#'), $atts, 'customLink');
$advcontent = get_field('acf_field', $current_user->ID);
$user = wp_get_current_user();
if (!empty($user->roles) && is_array($user->roles)) {
foreach ( $user->roles as $role ) {
if ($role=='my_custom_role') {
if ($advcontent == 'true') {
return "<a href='/my-link/'>".$atts['title']."</a>";
}
if ($advcontent == 'false') {
return "<a>".$atts['title']."</a>";
}
}
}
}
}
Iv also tried the below, but no luck:
add_shortcode('customLink', 'cm_link');
function cm_link($atts) {
$atts = shortcode_atts(array('title' => 'Custom Link', 'fallbackurl' => '#'), $atts, 'customLink');
$advcontent = $_POST['acf']['field_58061ec5608bd'];
if ($advcontent == 'true' ) {
return "<a href='/my-link'>".$atts['title']."</a>";
} else {
return "<a>".$atts['title']."</a>";
}
}
The true/false field doesn't return a value like that. It's basically returning a non empty value if checked, so get_field is all you need to do in order to return whether it's checked or not. If it's checked, then you just do
if ($advcontent) {
return "<a href='/my-link/'>".$atts['title']."</a>";
} else {
return "<a>".$atts['title']."</a>";
}
If you wanted a true/false as in your example, you would use a radio button with values set as true or false.

Moodle - Adding an element to the settings menu

I'm trying to develop a plugin in Moodle. One of the requirements is to add an element to the Settings Menu, in which I was able to achieve with the help of this guide
https://docs.moodle.org/dev/Local_plugins#Adding_an_element_to_the_settings_menu
And this is my code in local/myplugin/lib.php
<?php
function local_myplugin_extends_settings_navigation($settingsnav, $context) {
// question_extend_settings_navigation
global $CFG, $PAGE;
// Only add this settings item on non-site course pages.
if (!$PAGE->course or $PAGE->course->id == 1) {
return;
}
// Only let users with the appropriate capability see this settings item.
/*if (!has_capability('moodle/backup:backupcourse', context_course::instance($PAGE->course->id))) {
return;
}*/
if ($settingnode = $settingsnav->find('courseadmin', navigation_node::TYPE_COURSE)) {
$strfoo = get_string('classrecord', 'local_myplugin');
$url = new moodle_url('/course/classrecord.php', array('id' => $PAGE->course->id));
$foonode = navigation_node::create(
$strfoo,
$url,
navigation_node::NODETYPE_LEAF,
'myplugin',
'myplugin',
new pix_icon('i/grades', $strfoo)
);
if ($PAGE->url->compare($url, URL_MATCH_BASE)) {
$foonode->make_active();
}
$settingnode->add_node($foonode);
}
}
?>
I allowed the students to see the element "Class Record" in the settings menu
My concern is that how can I hide/show Class Record I added?
Any ideas would be great!
If you want only certain users to see the link, then create an appropriate capability in local/myplugin/db/access.php, e.g. 'local/myplugin:viewclassrecord', defaulting to being assigned to the 'student' role. Then check for it in the function you have defined.
e.g.
if (!has_capability('local/myplugin:viewclassrecord', $context)) {
return;
}

Buddypress: add custom component to members section

For each member page you have a selection of components (e.g. profile, settings, activity...)
What I am trying to do is add a new component called jobs to each page. I understand how to add the link to the navigation bar. It is just creating the page that is confusing me.
Do I add a new directory, if so how do I let buddypress know.
Having a url structure like:
example.com/member/username/jobs
is important.
Thanks!
The global variable $bp is where you want to insert the new Jobs component. By dumping the global variable $bp, you can see all of the elements contained in it, including all of the components. To easily dump $bp, add the following to the top of member-header.php:
global $bp;
foreach ( (array)$bp as $key => $value ) {
echo '<pre>';
echo '<strong>' . $key . ': </strong><br />';
print_r( $value );
echo '</pre>';
}
The array within $bp that you want to add the component 'Jobs' to is bp_nav.
In functions.php add the following:
add_action( 'bp_setup_nav', 'add_subnav_items', 100 ); //Priority must be higher than 10
function add_subnav_items() {
global $bp;
//Jobs tab
$tab_array['name'] = 'Jobs';
$tab_array['link'] = $bp->displayed_user->domain.'jobs';
$tab_array['slug'] = 'jobs';
$tab_array['parent_url'] = $bp->displayed_user->domain;
$tab_array['parent_slug'] = bp_core_get_userlink(bp_loggedin_user_id());
$tab_array['css_id'] = 'jobs';
$tab_array['position'] = 100;
$tab_array['user_has_access'] = '1';
$tab_array['screen_function'] = 'bp_jobs_screen_general';
$bp->bp_nav['jobs'] = $tab_array; //Add new array element to the 'bp_nav' array
}
The 'screen_function' is a function that handles the screen that is to be shown when 'Jobs' tab is selected so you must add the function 'bp_jobs_screen_general' in functions.php:
function bp_jobs_screen_general() {
bp_core_load_template( apply_filters( 'bp_jobs_screen_general','members/single/jobs' ) );
}
This function looks for a template file named jobs.php in members/single/ so you must create it. For an example on how the screen_function's work, refer to a function for displaying Groups screens within wp-content/plugins/buddypress/bp-groups/bp-groups-screens.php.

Autocomplete fields with database in Drupal content type

I started to create a new content type with modules Field Group and Field Collection and it's worked !
But, I would like to add autocomplete fields in my form and I don't find how to do this. Maybe using a hook_form_alter however I can't add my custom fields in my field collection.
You know how to do this ?
Thanks a lot
PS: I work with D7
To add an auto_complete to a from field, you need to do the following:
1st: use hook_for_alter() to add the auto_complete path to the text field
function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id)
{
if($form_id == "YOUR-CONTENT-TYPE_node_form")
{
$form['field_YOUR-FIELD']['und'][0]['value']['#autocomplete_path'] = 'my-module/autocomplete/path';
}
}
2nd: use hook_menu() to define the menu callback you passed as the #autocomplete_path property to the field
function YOUR_MODULE_menu()
{
$items = array();
$items['my-module/autocomplete/path'] = array(
'page callback' => 'your_module_autocomplete_callback',
'access callback' => TRUE,
'weight' => 1,
'type' => MENU_CALLBACK,
);
return $items;
}
3rd and last: add you menu callback function which returns the items to the textfield
function your_module_autocomplete_callback($string)
{
$items = array();
$query = db_select('node', 'n');
$value = $query->fields('n', array('title'));
$value = $query->condition(db_and()->condition('n.type', 'YOUR_CONTENT_TYPE')->condition('title', '%' . db_like($string) . '%', 'LIKE'))->orderRandom()->execute();
$i = 0;
foreach ($value as $val)
{
$items[$val->name] = check_plain($val->name);
}
print drupal_json_output($items);
exit();
}
Hope this helps... Muhammad.
the updated code that fits multiple fields.
function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id)
{
if($form_id == "YOUR-CONTENT-TYPE_node_form")
{
foreach($form['field_match']['und'][0]['field_adversaires']['und'] as $k =>$v)
{
if(is_numeric($k))
{
$form['field_match']['und'][0]['field_adversaires']['und'][$k]['value']['#autocomplete_path'] = 'basketfacile_type/autocomplete';
}
}
}
}
Hope it works for you... Muhammad.

Categories