query post to different role users in wordpress - wordpress-theming

Is it possible to show different posts to a different role users?? I need something like this
User A => Post A ||
User B => Post B
I can't use UserID, because there will be a lot of users. I was thinking something like this
<?php
if ( is_userA_logged_in() ) {
echo 'Welcome A user!'; //here comes the query
} elseif ( is_userB_logged_in() ) {
echo 'Welcome B user!'; // here comes the query
}
?>
I was reading on the codex but I only found the is_admin(), how can I call a new role?
I'd create the new roles in the functions.php with
<?php
add_role('userA', 'User A', array(
'read' => true, // True allows that capability
'edit_posts' => false,
'delete_posts' => false, // Use false to explicitly deny
));?>

See the current_user_can function.
if ( current_user_can( $role ) )
$q = get_query();
Also, check functions like get_currentuserinfo and get_userdata.

Related

ACF front end form to update term

I want to use ACF frontend form function to create a form with custom fields
I see this issue for create new term, #Alhana
ACF front end form to create term
but I want to generate the form with old data
Well, i didn't see that question, but if it's still actual, here's a solution.
First of all, make sure you have ACF group, linked to your taxonomy. You will need ID of this group, it can be found in url on group edit page, for example:
http://site.ru/wp-admin/post.php?post=340&action=edit
In this case group ID is 340. If you don't want to use hardcoded ID (if your groups are changing from time to time), you can get it, using group name (in this example group name is Technic CPT):
global $wpdb;
$group_ID = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = 'Technic CPT'" );
Then, you'll need ID of term you're updating. I think, it's not nesessary to write about getting it since it's WP basics :) You'll end with something like this:
$term_id = 405;
And finally, you'll need your taxonomy's slug. In this example it's technic. So, let's render our form!
acf_form_head();
$acf_form_args = array(
'id' => 'technic_edit_form',
'post_id' => 'technic_'.$term_id,
'form' => true,
'submit_value' => 'Update technic',
'field_groups' => array($group_ID),
'updated_message' => 'Technic is updated!';
);
acf_form( $acf_form_args );
Now your term's custom fields will be shown in this form. But to save term data after editing you'll need to add some more code. ACF form assumes that you're saving post data, we'll add some logic to detect saving data for term.
add_filter( 'acf/pre_save_post', 'acf_handle_form_save', 10, 1 );
function acf_handle_form_save( $post_id ) {
// Function accepts id of object we're saving.
// All WordPress IDs are unique so we can use this to check which object it is now.
// We'll try to get term by id.
// We'll get term id with added taxonomy slug, for example 'technic_405'.
// For checking term existence we must cut out this slug.
$cut_post_id = str_replace( 'technic_', '', $post_id );
$test_tax_term = get_term_by( 'id', $cut_post_id, 'technic' );
// If $test_tax_term is true - we are saving taxonomy term.
// So let's change form behaviour to saving term instead of post.
if ( $test_tax_term ) :
// Get array of fields, attached to our taxonomy
global $wpdb;
$group_ID = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = 'Technic CPT'" );
$acf_fields = acf_get_fields_by_id( $group_ID );
// Then sanitize fields from $_POST
// All acf fields will be in $_POST['acf']
foreach ( $acf_fields as $acf_field ) :
$$acf_field[ 'name' ] = trim( esc_attr( strip_tags( $_POST[ 'acf' ][ $acf_field[ 'key' ] ] ) ) );
endforeach;
// We need to have some fields in our group, which are just duplicates of standard term fields: name, slug, description.
// In this example it's only one field - term name, called 'technic_name'.
$name = 'technic_name';
// Update base term info, in this example - only name.
$term = wp_update_term( $cut_post_id, 'technic', array( 'name' => $$name ) );
// If all is correct, update custom fields:
if ( !is_wp_error( $term ) ) :
foreach ( $acf_fields as $acf_field ) :
update_field( $acf_field[ 'name' ], $$acf_field[ 'name' ], 'technic_' . $cut_post_id );
endforeach;
endif;
else :
// Here is saving usual post data. Do what you need for saving it or just skip this point
endif;
return $post_id;
}
Please note: validation of $_POST data may be more complex. For example, you may have to validate array of values if there are ACF galleries or relationships among your taxonomy fields. In my example i have only common text fields.
Hope that helps!
The answer from Alhana worked for me with one change. The term object works if sent as the the value for the post_id:
$term_obj = get_term($term_id);
$acf_form_args = array(
'post_id' => $term_obj,
'post_title' => false,
'submit_value' => 'Update Term',
'field_groups' => array($group_ID),
);

Show message if no orders in woocommerce

I would like to display a message in my-account if the customer has no orders I would like to show "No orders currently".
I guessing there's a function I could use to hook into this somewhere?
Had a good search and cant find anything to get me started.
Thanks.
Modifying from this tutorial I think this would work:
function wc_get_customer_orders() {
// Get all customer orders
$customer_orders = get_posts( array(
'numberposts' => 1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_order_statuses() ),
) );
$customer = wp_get_current_user();
// Text for our message
$notice_text = sprintf( 'Hey %1$s 😀 We noticed you haven\'t placed any orders with us.', $customer->display_name );
// Display our notice if the customer has no orders
if ( count( $customer_orders ) == 0 ) {
wc_print_notice( $notice_text, 'notice' );
}
}
add_action( 'woocommerce_before_my_account', 'wc_get_customer_orders' );
Basically, on the account page we query for a single order by the currently logged in user. If we don't get an order back we show the notice.
Copied my-orders.php to local folder and added an else clause:
else{
echo "No orders";
}
This can be styled using WC css

How to set Authorization wise view in the Form in CakePHP-3.0

I want to set an authorization in the registration of a form using CakePHP 3.0.
Before asking here, i tried below things but no luck in my favor.
Suppose i have role field in the usersTable like 'superuser', 'admin', 'user'.
I want to provide permission superuser to make all the things like create admin and user. and then admin can create admin and user, and user can make user only.
The code i tried in the add function of UsersController.
if($this->Auth->user['role'] === 'superuser'){
$roles = $this->Users->find('list');
} elseif ($this->Auth->user['role'] === 'admin') {
$roles = $this->Users->find('list')->where(['Users.role !==' => 'superuser']);
} else {
$roles = $this->Users->find('list')->where(['Users.role' => 'user']);
}
after failing i tried below things in the add.ctp
if(!empty($this->request->session()->check('Auth.User.role') === 'superadmin')){
echo $this->Form->input('role',['options' => ['admin' => 'Admin', 'user' => 'User']]);
} elseif(!empty($this->request->session()->check('Auth.User.role') === 'admin')){
echo $this->Form->input('role',['options' => ['user' => 'User', 'icr' => 'ICR', 'routing' => 'Routing']]);
} else {
echo $this->Form->input('role', ['options' => ['user' => 'User']]);
}
Could you please suggest on this regard or is there any easiest way to do so?
Thanks
As outlined in your double post # google groups ( https://groups.google.com/forum/#!topic/cake-php/nTURwYME-6o ) you shoudn't blindy combine three things that have nothing in common, that is guaranteed to blow up.
if(!empty($this->request->session()->check('Auth.User.role') === 'superadmin')){}
should rather be
if ($this->request->session()->read('Auth.User.role') === 'superadmin') {}

Table which can add, edit ,and delete data dynamically in Drupal

I know that we can add edit and delete data in a table statically in Drupal. But is there any way we can add edit and delete data via clicking a link just near to each row so that on clicking "add " button should generate a new row, and on clicking edit should highlight all the contents of the row as fr editing and delete should remove the row. The basic table I created is this:
<?php
$header = array('Emp ID', 'Emp Name', 'Emp Age');
$rows = array();
$sql = 'SELECT empid, name, age FROM {employee} ORDER BY name';
$result = db_query($sql);
while ($row = db_fetch_array($result)) {
$rows[] = $row;
}
print theme('table', $header, $rows);
?>
You can build it. You've already got your Read mode, so you need a Create/Update/Delete... there is some help on the internet for basic CRUD modules. Very basically, the update part, (first having a menu hook using % wildcard which is what arg(1) will be:
function crud_module_edit_form() {
$result = db_select('employee', 'e')
->fields('e', array('empid', 'name'))
->condition('empid', arg(1), '=')
->orderBy('name', 'DESC')
->execute()
->fetch();
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => isset($result->name) ? $result->name : '',
'#required' => TRUE,
);
return $form;
}
Then the submit function for the above form to update the record. You could use db_merge here instead and then use the same page for both the Add and the Update functions.
function crud_app_edit_form_submit($form, &$form_state) {
db_update('employees')
->fields(array(
'name' => check_plain($form_state['values']['name']),
))
->condition('empid', arg(1), '=')
->execute();
drupal_set_message(t('Employee updated'));
}

Export list of pretty permalinks and post title

Looking for a way to export a list of pretty permalinks in WordPress with the corresponding post title. Looking for the actual permalink structure defined not the shortlink. I suppose if I have to, I will use a short link, but I prefer the full permalink.
Here's a standalone PHP file you can save into the root of your website called something like /export.php and when you call it with your browser it will send a tab-delimited plain text list of posts with the pretty permalink, the post title and (as a bonus) the post type.
Just load the URL in your browser and then "save as" to a text file you can then load in Excel or however else you need to process it.
<?php
include "wp-load.php";
$posts = new WP_Query('post_type=any&posts_per_page=-1&post_status=publish');
$posts = $posts->posts;
/*
global $wpdb;
$posts = $wpdb->get_results("
SELECT ID,post_type,post_title
FROM {$wpdb->posts}
WHERE post_status<>'auto-draft' AND post_type NOT IN ('revision','nav_menu_item')
");
*/
header('Content-type:text/plain');
foreach($posts as $post) {
switch ($post->post_type) {
case 'revision':
case 'nav_menu_item':
break;
case 'page':
$permalink = get_page_link($post->ID);
break;
case 'post':
$permalink = get_permalink($post->ID);
break;
case 'attachment':
$permalink = get_attachment_link($post->ID);
break;
default:
$permalink = get_post_permalink($post->ID);
break;
}
echo "\n{$post->post_type}\t{$permalink}\t{$post->post_title}";
}
Hope this helps.
-Mike
P.S. I used the standard WordPress WP_Query() but also included a commented-out SQL in case you prefer (or need) to use it instead.
answered this one on EE this morning :)
http://wp.daveheavyindustries.com/2011/02/08/wordpress-permalink-via-sql/
this query should do it for you
SELECT wpp.post_title,
wpp.guid,
wpp.post_date,
CONCAT
(
wpo_su.option_value,
REPLACE
(
REPLACE
(
REPLACE
(
REPLACE
(
wpo.option_value,
'%year%',
date_format(wpp.post_date,'%Y')
),
'%monthnum%',
date_format(wpp.post_date, '%m')
),
'%day%',
date_format(wpp.post_date, '%d')
),
'%postname%',
wpp.post_name
)
) AS permalink
FROM wp_posts wpp
JOIN wp_options wpo
ON wpo.option_name = 'permalink_structure'
AND wpo.blog_id = 0
JOIN wp_options wpo_su
ON wpo_su.option_name = 'siteurl'
AND wpo_su.blog_id = wpo.blog_id
WHERE wpp.post_type = 'post'
AND wpp.post_status = 'publish'
ORDER BY wpp.post_date DESC
I also wanted this solution and thanks #MikeSchinkle for the original solution. I did use this to export those links in plain text to excel and then build my redirect list.
But then I found that I also wanted a solution with live, active links.
So I used the wp_query using the post type "any" and created a page template with a search form included with the following query (customize to fit your theme as you see fit). Note I had to set the posts_per_page at -1 to return unlimited results. This returns results as: "Title - Permalink"
<?php
$type = 'any';
$args = array (
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'DESC',
);
$temp = $wp_query; // assign ordinal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<?php the_title(); ?> - <?php the_permalink() ?><br />
<?php endwhile; ?>
<?php else :
echo '<h2>Sorry, we didnt find any results to match. Please search again below or call us at 800-828-4228 and we will be happy to help!</h2>';
get_search_form();
endif;
$wp_query = null;
$wp_query = $temp; // Reset
?>
Hope that helps others.