I have created a module in Magento which takes certain views from customers about any product and displays them as extra information under each product when its approved by the admin from back end.
Now I have got three fields Name, Email N Views. I would like to know how to keep a customer anonymous in this form if he wishes not to display his name when his views are approved and posted on the product page.
Thanks.
I found a solution by stumbling upon original core files. I added a checkbox in the form on the front end.
<li class="control"><input type="checkbox" name="is_anonymous" id="anonymous" value="1" title="<?php echo $this->__('Is Anonymous') ?>"<?php if($this->getIsAnonymous()): ?> checked="checked"<?php endif; ?> class="checkbox" /><label for="anonymous"><?php echo $this->__('Is Anonymous') ?></label></li>
I added the is_anonymous column in the table and in the core controller file I added this to save the preference of each user
if($post = $this->getRequest()->getParam('is_anonymous'))
{ $record->setIsAnonymous(1);
else
{ $record->setIsAnonymous(0); }
And then to display it on front end I added an extra condition in list.phtml
<small class="by"><?php echo $this->__('By: '); ?><?php if($entry->getIsAnonymous()==1) {echo 'Anonymous';} else { echo $entry->getName();} ?></small>
And this lets me have a check box which if ticked displays the post as 'Anonymous' by user who has posted or else displays his name on the front end.
Hope this helps some one!
Related
I have plugin A that needs to verify two pieces of shortcodes before an action takes place within another plugin.
<?php
function mepr_add_some_tabs($user) {
?>
<span class="mepr-nav-item prem-support <?php MeprAccountHelper::active_nav('member-profile'); ?>">
<!-- KEEPS THE USER ON THE ACCOUNT PAGE -->
Profile
</span>
<?php
}
add_action('mepr_account_nav', 'mepr_add_some_tabs');
function mepr_add_tabs_content($action) {
//Listens for the "member-profile" action on the account page, before rendering the FF view shortcode.
if($action == 'member-profile') {
echo do_shortcode('[frm-condition source=frm-stats id=80 type=count user_id=current greater_than=0] [display-frm-data id=5728] [/frm-condition]');
}
}
add_action('mepr_account_nav_content', 'mepr_add_tabs_content');
The problem is I'm not a developer and I don't know how to add the second shortcode in the echo do_shortcode function.
The current shortcode basically says: if the user has an entry on the field ID: 80, then show them the content of the entry.
What I need to add is another, second shortcode which will say almost the same thing, but about another field ID.
How can I add it while following this requirement?
I have returned a list of registered users. I am looking to be able to query these results and also generate a click through page. Is that possible.
Im trying to essentially get a list of users by searching (first name for example), then click through to show information about them, on the front end. Its mainly the click through im having difficulty with.
For example if i could click a member and it would go through to a page that had the user first name, last name etc.
<?php
$blogusers = get_users();
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
if ( in_array( 'team_member', (array) $user->roles ) ) {
?>
<div class="member_user">
<a href="<?php echo the_permalink();?>">
<?php
echo $user->first_name." ".$user->last_name;
?>
</a>
</div>
<?php
}
}
?>
If i understood correctly, you already have a list of users, and you want each one of them to be linkable to its own page, where you would display the info you want, such as first name, last name etc.
Firstly you have to query for your users and loop through them to display them in a list. You already have this in your code, but if you want something more advanced a query would look like this:
$authors = get_users(array(
'role__not_in' => array('Subscriber', 'Administrator'),
//if you do not want to list the admins and subscribers
'orderby' => array(
'post_count' => 'DESC',
//order users by post count (how many posts they have)
)
));
foreach ($authors as &$author){
//loop through the users
$user_info = get_userdata($author->ID);
$user_link = get_author_posts_url($author->ID);
?>
<a href="<?php echo $user_link; ?>" style="display:block;">
<?php echo $user_info->first_name.' '.$user_info->last_name; ?>
</a>
<?php
}
Then you need to create the page for the user. WordPress handles this by author.php page. If your theme does not have one, simple create a php file called author.php and place it at the root folder of your theme.
You may edit author.php to include the data you want. However if you remove the list of posts that are displayed by default for the selected user you will loose this functionality in your theme.
So inside author.php you may get the user like so:
//get current user (object):
$current_author = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
//get User data by user ID
$user_info = get_userdata($current_author->ID);
//echo first & last name:
echo $user_info->first_name.' '.$user_info->last_name;
If you make a print_r($user_info) you may see what info you can display for the selected user.
You may also use the 'get_the_author_meta' function to get more info from the user's profile, eg
get_the_author_meta('description', $current_author->ID);
You can find more info about this function here: https://developer.wordpress.org/reference/functions/get_the_author_meta/
I would generate a link like this:
<?php
echo $user->first_name." ".$user->last_name;
echo 'view user';
?>
And then query the user ID on the next page to get the desired information.
On the next page you can:
<?php
$user_ID = $_GET['user_id'];
get_header();
?>
and then query this to get pretty much any content related to that user
Two and a half approaches:
You could customize your theme's author archive template to display the desired information. Three author archives are available in the template hierarchy:
author-{nicename}.php
author-{id}.php
author.php
You'd be able to link to https://{site_url}/author/{username} for each user.
However, this may not be ideal if you want to keep that normal author blog archive and add the new profile pages. So what may be better long term is to setup a custom post type for the member profiles, then every time a new "member" user is created run a function that creates a new post representing the user.
You can register that custom post type manually with register_post_type() or if you need help a good plugin is Custom Post Type UI.
A good action for your function to hook into is user_register since it happens immediately after user registration but will have access to user data. Within your function you can create the new post using wp_insert_post().
You can then display the information of that custom post type using the archive-$posttype.php and single-$posttype.php templates.
2 1/2. You may also consider using only the custom post type and not querying for users at all. If this member profile is the only reason some folks are users it may even be more convenient this way. Then you can use the archive-$posttype.php and single-$posttype.php templates easily to display the information you choose.
There is a function for that. Use get_author_posts_url()
In the required template use the following code:
// only return required user based on role
$args = array(
'role' => 'team_member'
);
$blogusers = get_users( $args );
// loop though and create output
foreach ( $blogusers as $user ) : ?>
<div class="member_user">
<a href="<?php echo get_author_posts_url( $user->ID );?>"> // links to author page
<?php echo $user->first_name." ".$user->last_name; ?>
</a>
</div>
<?php endforeach;
You will then need to customise the author.php template in your theme to render as required.
I would like to show a custom welcome message for each of my registered shop customers. Something like "Welcome CUSTOMERNAME! You have been with us for one year now. To say thanks, we are giving you a discount on everything in our shop of 10%."
I have created a custom field welcome_message with the plugin Advanced Custom Fields and would like to show the value of it in the account dashboard of my customers at frontend.
At the backend, I am showing the textarea at the user profile page and I am adding the field with this code:
$message = get_field( 'welcome_message' );
echo esc_attr( $message );
This code is placed here: /wp-content/themes/flatsome-child/woocommerce/myaccount/dashboard.php
But somehow the value is not coming up. I have also tried it with simply the_field('welcome_message'); or to add [acf field="{$welcome_message}"] at the account page in WordPress but that didn't work either. Somehow the value is not coming through.
I hope someone could help me out a litte.
Thanks in advance.
Best regards
It's necessary that get_field function is connected to user.
Eg:
<?php
$variable = get_field('field_name', 'user_1');
?>
In case of repeater field:
<?php if( have_rows('repeater', 'user_1') ): ?>
<ul>
<?php while( have_rows('repeater', 'user_1') ): the_row(); ?>
<li><?php the_sub_field('title'); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
I want my users to be able to put a Contact Form 7 shortcode into a custom field in the Wordpress editor. I've created the custom field using ACF and I can pull the value onto the page, but when I try to include it in the shortcode, it comes back with a 404.
This code:
<?php echo do_shortcode(get_field('contact_form_shortcode')); ?>
Returns:
[contact-form-7 404 "Not Found"]
If I create a variable out of the value like this:
<?php
$formCode = get_field('contact_form_shortcode');
echo $formCode;
?>
The echo returns:
[contact-form-7 id="473" title="Learn More Form"]
But I get the same 404 after putting that value into the echo do_shortcode function list this:
<?php echo do_shortcode($formCode); ?>
What am I missing?
To do it With ACF pro plugin and without other extra plugins.
Create a relation field ( example: contact_form )
add the below code into your page loop:
<?php $posts = get_field('contact_form');
if( $posts ):
foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT)
$cf7_id= $p->ID;
echo do_shortcode( '[contact-form-7 id="'.$cf7_id.'" ]' );
endforeach;
endif; ?>
I was able to resolve this issue by using the technique I discussed in my comment above. By using the WYSWIG field set to 'Run filter "the_content"' I'm able to pull the field value in the way I want it. The only drawback is that users could type something else in there besides a form shortcode.
Here's my final code:
<?php
if (get_field('contact_form_shortcode')):
echo get_field('contact_form_shortcode');
else:
echo do_shortcode('[contact-form-7 id="473" title="Learn More Form"]');
endif;
?>
Instead of using a WYSIWYG field, you can simply set a Text field to "Convert HTML into tags" under the Formatting setting on the field. This will stop the 404 errors from CF7 and properly process the form. The WYSIWYG fields tend to be too hard to control from bad user input.
Here's my solution:
Create a "Post Object" type Field which will returns the post ID.
the Filter by Post Type for this field should be sett to "Contact Form" and Filter by Taxonomy leave empty.
Then in page template: (php)
<?php $contact_form = get_field('contact_form'); ?>
HTML:
<div class="col-12 col-md-8 offset-md-2">
<?php echo do_shortcode('[contact-form-7 id="'. $contact_form .'"]'); ?>
</div>
acf field screenshot
Another Solution :
1) Install https://github.com/taylormsj/acf-cf7
Installation
> 1 Copy the acf-cf7 folder into your wp-content/plugins folder
> 2 Activate the Contact Form 7 plugin via the plugins admin page
> 3 Create a new field via ACF and select the Contact Form 7 type
> 4 Please refer to the description for more info regarding the field type settings
2) Insert following code into template :
<?php if( get_field('field_acf') ):
the_field('field_acf');
endif; ?>
This applies to create a contact form 7 in a modal or Pop Up, multiple forms on the same page.
To paste CF7 shortcode (or any other generated shortcode) into ACF field as you were asking, just use the #armadadrive solution, which is exactly what question was about and it works.
create ACF text field with some name, eg hero_form
paste your shortcode into it
display contents like so <?php echo do_shortcode(get_field('hero_form')); ?>
Thanks #armadadrive
Here you can try please check the code for the shortcode below:
<?php echo do_shortcode(get_field('YourACFfieldNameHere')); ?>
I tried it on my website and it works.
I am using a custom field plugin for Wordpress so that users of the CMS can keep post items organized. I setup the fields and in my loop I display the like this..
<p><strong>Business Phone:</strong> <?php the_field('business_phone'); ?></p>
How would I hide or change the color of the title if there is no information entered into the field? I've gotten this far. Am I on the right track?
<?php
if ( get_group('business_phone',TRUE) ) {
echo 'I see a phone number!!';
} else {
echo 'I don\'t see a phone number!!';
}
?>
ok.. I'll check out the Wordpress forum.