Can we customize the users list view in the admin? - php

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

Related

how to loop OR WHERE in codeigniter according to the data selected on MULTISELECT

I want to make a data filter feature using the MULTISELECT category where the data that appears is in accordance with the list of categories selected on the filter. So, I plan to loop the OR WHERE command according to the list of categories chosen by the user. OR WHERE has been successfully loop according to the number of categories chosen, but not for the ID. And show error like this "Array to string conversion"
What is the cause and what is the solution? Thank you
My Controller
$sort = $this->input->post('sort');
$category = $this->input->post("get_kategori_acara_css[]");
$interest = $this->input->post("get_interest_acara_css[]");
/* eksekusi library pagination ke model penampilan data */
if (!empty($sort) || !empty ($category) || !empty ($interest))
{
$this->data['list_acara'] = $this->Acara_model->get_filtered($sort, $category, $interest, $config['per_page'], $dari);
}
else
{
$this->data['list_acara'] = $this->Acara_model->get_all_arsip();
}
MY MODEL
public function get_filtered ($sort, $category, $interest) {
$this->db->join('kategori', 'acara.id_kategori = kategori.id_kategori');
$this->db->join('interest_acara ', 'acara.id_acara = interest_acara.id_acara', 'left');
$this->db->join('hits_acara ', 'acara.judul_seo = hits_acara.judul_seo', 'left');
$this->db->where('publish','Ya');
$this->db->group_by('acara.judul_seo');
if(!empty($sort AND empty($category) AND empty($interest)))
{
$this->db->order_by($sort, $this->order);
}
elseif(!empty($sort) AND !empty($category) AND empty($interest))
{
foreach ($category as $value) {
$this->db->or_where('acara.id_kategori', $value->id_kategori);
}
}
return $this->db->get($this->table)->result();
}
Shown Error
My Multiselect Feature
The problem is the use of $value->id_kategori in the foreach ($category as $value) loop.
The variable $value is not an object so trying to get the value of the property, i.e. $value->id_kategori makes no sense.
$category is a simple indexed array of values e.g.
array('0'=>'123', '1'=>'456', '2'=>'789')
I think all you need to do is change this
$this->db->or_where('acara.id_kategori', $value->id_kategori);
to this
$this->db->or_where('acara.id_kategori', $value);

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.

Registration form fields reset after submit (wordpress & woocommerce)

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.

Fetching revolution slider shortcodes

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));
}

Limit user role on multiple field

I know that I can make two different field types and assign different roles to them.
Because of using views, custom searches, and gmaps, and because this will be applied to Field collection I really need this kind of approach to the problem.
I tried to crate custom module that will limit role staff to only be able to insert two field_text into node but administrator can insert as many as he wont. field_text Number of values is set to Unlimited and Content type name is youtube.
I found this for drupal 6 but I don't know how to code it in drupal 7.
function myformlimit_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'youtube') {
global $user;
// Only allow to insert 2 text for role = staff
if (in_array('staff', $user->roles)) {
$text_limit = 2;
$form['#field_info']['field_text']['multiple'] = $text_limit;
$i = 1;
foreach ($form['field_text'] as $key => $value) {
if (is_numeric($key)) {
if ($i > $text_limit) {
unset($form['field_text'][$key]);
}
$i++;
}
}
}
}
}
Well you are half way there. You can use the following code to get you the idea.
Kindly note that I named my field field_test_field, you can replace that with your field's name.
function myformlimit_form_alter(&$form, &$form_state, $form_id)
{
if($form_id == 'youtube_node_form')
{
global $user;
if(in_array('staff', $user->roles))
{
if($form_state['field']['field_test_field'][LANGUAGE_NONE]['items_count'] >= 2)
{
unset($form['field_test_field'][LANGUAGE_NONE]['add_more']);
}
}
}
}

Categories