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.
Related
My Wordpress text field package_id in the CPT gd_place should accept data only from a finite list of known and trusted values which are different depending on user e.g. role_one can only input 1, role_two can only input 2, role_tree only 3.
I first wrote this function, hiding the input field with css
function safe_package( $data ) {
if( 'gd_place' != $data['post_type'] ){
return $data;
//This is for one CPT only
}
$user = wp_get_current_user();
if(!is_admin() && !current_user_can('administrator')){
//Does NOT run in the backend and also takes the admin role into account
if(current_user_can('role_one')){
$data['package_id'] = 1;
return $data;
}
else if (current_user_can('role_two')){
$data['package_id'] = 2;
return $data;
}
else if (current_user_can('role_three')){
$data['package_id'] = 3;
return $data;
}
} else {
//do nothing new
return $data;
}
}
add_filter( 'wp_insert_post_data', 'safe_package' );
But then it might make more sense to just validate the user input (whitelist) and update_post_meta( $post->ID, 'package_id', $safe_package );
As the options are finite per user role maybe just replacing any wrong input would make sense... anyone familiar with validation?
I'm trying to redirect to a custom URL after the confirmation in gravity forms, I set this in functions.php but I need to fix what is wrong with the code, I need to do it this way since I have 2 domains sharing the same form.
What I did was set a hidden field in the form with Default Value {referer}, and also allowed the field to be populated dynamically with parameter 'refurl':
add_filter('gform_confirmation', 'conditional_confirmation', 10, 4);
function conditional_confirmation($confirmation, $refurl, $entry, $ajax) {
if ($refurl == 'http://www.example.com') {
$confirmation = array('redirect' => 'http://www.google.com');
}
return $confirmation;
}
When clicking the confirmation button the form doesn't redirect anywhere. Do you guys know what is wrong and how to fix it?
As per the documentation, second argument for the filter is a $form object. So you need to get your hidden input value from the $form object.
Give an input name to that hidden field as 'refUrl' in the gravity field properties.
add_filter('gform_confirmation', 'conditional_confirmation', 10, 4);
function conditional_confirmation($confirmation, $form, $entry, $ajax) {
$input_name = 'refUrl';
foreach ($form['fields'] as $field) {
if (isset($field['inputName']) && $field['inputName'] === $input_name) {
$fieldName = 'input_' . $field['id'];
$refurl = $_POST[$fieldName];
break;
}
}
if ($refurl == 'http://www.example.com') {
$confirmation = array('redirect' => 'http://www.google.com');
}
return $confirmation;
}
I am trying to customize the view of the users list page, i want to display only the rows with the meta value equals to Mycenter. I can do it using jQuery but it will no be secure.
Is there any filter to help me do it using php instead?
You can do that using the manage_users_custom_column filter:
exp:
add_filter('manage_users_custom_column',
function($value, $column_name, $user_id) {
if ( 'center' == $column_name ) {
if ('Mycenter' == $value) {
return $value;
} else {
return "";
}
}
}
, 20, 3);
more info manage_users_custom_column
I've got woocommerce registration form with two sections:
- One for private person,
- the other for company.
In company option there is two additional fields. I can switch between private and company by radio buttons and then I see relevant fields.
Problem: When I fill the form (as private user) and make some mistake, form reload and show where is the error (that is ok).
But unfortunately, after reload, it loads the form with all fields (the ones with additional company fields too). So I need to click 2 times between private and company to restore the right behavior.
How can i fix this? I mean after this error reloading, to display the form as initially.
I don't be sure that this is code responsible for this, but let's try:
add_filter('woocommerce_registration_errors', 'rs_registration_form_validation', 10, 3);
function rs_registration_form_validation($reg_errors, $sanitized_user_login, $user_email)
{
global $woocommerce;
$company_fields_required = (!empty($_POST['registration_type']) && 'company' === $_POST['registration_type']);
$shipp_to_different_address = (!empty($_POST['register_ship_to_different_address']) && 1 == $_POST['register_ship_to_different_address']);
$errors = false;
$fields = rs_registration_form_fields();
if ($shipp_to_different_address) {
$fields += rs_registration_form_fields_address();
}
if (!$company_fields_required) {
unset($fields['billing_company']);
unset($fields['billing_nip']);
}
//Validate required
foreach ($fields as $field => $settings) {
if (false === isset($settings['required']) || true === $settings['required']) {
if (empty($_POST[$field])) {
$errors = true;
wc_add_notice('Pole: <strong>'.$settings['label'].'</strong> jest wymagane.', 'error');
}
}
}
if ($errors) {
return new WP_Error('registration-error', 'Proszę poprawić błędy w formularzu');
}
return $reg_errors;
}
add_action('woocommerce_created_customer', 'rs_registration_form_submit');
function rs_registration_form_submit($user_id)
{
$fields = rs_registration_form_fields();
$fields += rs_registration_form_fields_address();
foreach ($fields as $field => $settings) {
if (isset($_POST[$field]) && !empty($_POST[$field])) {
update_user_meta($user_id, $field, $_POST[$field]);
}
}
}
add_filter('register_form', 'rs_registration_form');
function rs_registration_form()
{
$fields = rs_registration_form_fields();
include '_rs_registration_form.php';
}
add_filter('register_form_address', 'rs_registration_form_address');
function rs_registration_form_address()
{
$fields = rs_registration_form_fields_address();
include '_rs_registration_form.php';
}
add_filter('woocommerce_edit_address_slugs', 'rs_fix_address_slugs');
function rs_fix_address_slugs($slugs)
{
$slugs['billing'] = 'billing';
$slugs['shipping'] = 'shipping';
return $slugs;
}
function rs_rejestracja_url()
{
return get_permalink(244);
}
function rs_logowanie_url()
{
return get_permalink(20);
}
function rs_show_checkout_progress_bar($step = '')
{
include '_checkout_progress_bar.php';
}
function rs_order_form_buttons()
{
include '_order_form_buttons.php';
}
add_filter('woocommerce_get_checkout_url', 'rs_get_checout_url');
function rs_get_checout_url($url) {
if (is_user_logged_in()) {
$url .= '#step1';
}
return $url;
}
include 'src/RS_Search.php';
I don't know WooCommerce, but I think the error results because of these lines:
$company_fields_required = (!empty($_POST['registration_type']) && 'company' === $_POST['registration_type']);
and
if (!$company_fields_required) {
unset($fields['billing_company']);
unset($fields['billing_nip']);
}
After you submitted your "private" form and the validation failed, your fields are loaded again. Could it now be, that in your $_POST variable the registration_type is somehow set to 'company'? You can test this if you just print_r your $_POST['registration_type'] at the beginning of the function. If that is not the case, then I'm pretty sure the bug happens in another function, because this makes sense to me so far.
EDIT: After taking another look on your code, I think none of the posted functions is responsible for the misbehaviour. The first function is only responsible to check if some of the posted values are missing and to say "hey, here is an error". There has to be another function which is responsible for the fields which later are displayed in your HTML. I think you need to find this function.
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));
}