I'm trying to implement in the code a way to show specific contact forms data to a particular user via ID but I'm having difficulty finding which part of the code to add it to.
$user_ID = get_current_user_id();
if ( $user_ID == ('2') ) {
I'm currently inside the admin-mainpage.php within the plugin files.
It's not as simple as I thought as it doesn't store the form names via db.
Any help would be appreciated.
Forms-list
It looks like Contact Forms 7 is your plugin for the forms portion while CFDB7 is an accompanying plugin that writes the form submissions to the WP database.
Get the Form ID
There are a couple ways to get the form ID. It looks like the easiest path is to look at the shortcode that the CF7 creates. You can see it in their documentation here. The code is something like [contact-form-7 id="4" title="Foo"] with id being the form ID (4 in this case).
Show the Right Form to the Right Person
Assuming you know the user ids and the related form ids you want to show them, you could write a very simple shortcode plugin to display the right forms for the right people. It'd be something like this untested code below.
//[user-form-display]
function user_form_display( $atts ){
$user_id = get_current_user_id();
if ($user_id == 2){
echo do_shortcode('[contact-form-7 id="4" title="Foo"]');
} else if ($user_id == 4){
echo do_shortcode('[contact-form-7 id="88" title="Bar"]');
}
}
add_shortcode( 'user-form-display', 'user_form_display' );
You could then put the shortcode in the regular post field and not edit either the CF7 plugin nor mess with the theme files.
You could also make the shortcode fancier and tie user ids to form ids directly in the shortcode arguments. That would take a bit more effort but is probably worth it.
Getting the Form Data
You could modify the $args to include form ids based on an association with a user id or multiple user ids. The form ids should be a field in that table. That's the example indicated below.
Alternately you could modify how the information is returned based on the same relationships by setting up the if/then statements in the $data_value lines. This is easier probably but messier in the long run.
function specfic_table_data()
{
global $wpdb;
$user_id = get_current_user_id();
if($user_id == 1){
$form_ids = array(4,6);//only returns forms with id 4 and 6 when it's user w id 1
}
$cfdb = apply_filters( 'cfdb7_database', $wpdb );
$data = array();
$table_name = $cfdb->prefix.'db7_forms';
$args = array(
'post_type'=> 'wpcf7_contact_form',
'order' => 'ASC',
'posts_per_page' => 10,
'post__in' => $form_ids,
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
$form_post_id = get_the_id();
$totalItems = $cfdb->get_var("SELECT COUNT(*) FROM $table_name WHERE form_post_id = $form_post_id");
$title = get_the_title();
$link = "<a class='row-title' href=admin.php?page=cfdb7-list.php&fid=$form_post_id>%s</a>";
$data_value['name'] = sprintf( $link, $title );
$data_value['count'] = sprintf( $link, $totalItems );
$data[] = $data_value;
endwhile;
var_dump($data);
}
Related
I am using https://wordpress.org/plugins/tangible-loops-and-logic/ to show a list of posts. Each post also contains a Contact Form 7 form. That form has a dropdown which should be populated by an acf repeater field that is attached to each post.
My issue is that in order to retrieve the correct values I need to get the correct post id for each post. I know I can create a dynamic field in cf7 to show the post ID but I'm not really sure how I can pass that around so that I can retrieve the correct repeater.
My loop (simplified):
<Loop type=seminar count=3>
<Field title />
<div class="seminarContent">
...
</div>
[contact-form-7 id="619" title="Seminar"]
</Loop>
The select field of my form:
[select termine data:gigs]
my current code to retrieve the post ID which would work if it was on the single post page, but unfortunately not within a loop showing all posts on the same page as discussed here https://stackoverflow.com/a/71840727/6118046:
add_filter( 'wpcf7_form_tag_data_option', 'dd_filter_form_tag_data', 10, 3 );
function dd_filter_form_tag_data( $n, $options, $args ) {
// Get the current form.
$cf7 = wpcf7_get_current_contact_form();
// Get the form unit tag.
$unit_tag = $cf7->unit_tag();
// Turn the string into an array.
$tag_array = explode( '-', $unit_tag );
// The 3rd item in the array will be the page id.
$post_id = substr( $tag_array[2], 1 );
if ( in_array( 'gigs', $options, true ) ) {
$gigs = array();
if ( have_rows( 'termine', $post_id ) ) :
while ( have_rows( 'termine', $post_id ) ) :
the_row();
$gigs[] = get_sub_field( 'termin' );
endwhile;
endif;
$n = array_merge( (array) $n, $gigs );
}
return $n;
}
Seems to work if I use the previous version of the plugin, called "custom content shortcode". Then add a custom field (text) to my post in which I paste the cf7 shortcode (same for all posts) and then call the field in the frontend. Dropdown gets properly filled that way.
I'm trying to save Query Vars to a post for later retrieval.
I'm using permalinks in this format: domain.com/%category%/%postname%/
Example:
I create a following page
domain.com/page-003/
I add Query Var called email to the page
add_query_arg('email', 'test#abc.com', 'domain.com/page-003/')
Now when I call
get_permalink($post_id);
I get
domain.com/page-003/
Instead of
domain.com/page-003/?email=test#abc.com
What am I missing? Aren't Query Vars saved with a post?
You want to save some meta data which you want to restore later on and add as query arg into the URL.
You need to first save it as post_meta like e.g. when you save post with that data. You use:
<?php update_post_meta(get_the_ID(), 'email_address', 'abc#mail.com'); ?>
More details: https://codex.wordpress.org/Function_Reference/update_post_meta
Then during the retrieval, you may hook into a HOOK early on like template_redirect or earlier of the post you can get post_meta to get the email and then add to query arg:
<?php $email = get_post_meta( get_the_ID(), 'email_address' ); ?>
Then
esc_url( add_query_arg( 'email', $email, get_permalink( get_the_ID() ) ) );
Something like that, code is untested, I just wrote here, you may please read detailed doc in codex for each function used above.
Update: How to update/fill Ninja form field from Meta Value:
add_filter( 'ninja_forms_render_default_value', 'wm_the_value' , 10 , 3);
function wm_the_value( $default_value, $field_type, $field_settings ) {
if( 'textbox' == $field_type && in_array('ref' , $field_settings)){
$default_value = get_post_meta(get_the_ID(),'_listing_mls', true);
}
return $default_value;
}
'ref' is field name in Ninja form.
'_listing_mls' is meta_key name from WP database.
Hope it works for you.
I'm using WordPress with a template that generates a pretty nice thumbnail for each post depending on Ids, and type of post. (ref:https://blinkdemo.wordpress.com/)
Since I've been asked to create a custom page that could show certain post from a category, I decided to create a query for a template page that checks the page slug and then list the posts containing a certain category + a tag ('comparativas').
The problem that I'm facing is that the list of post presented on the page doesn't show up the corresponding thumbnail on each post.
Thumbnails are generated dynamically basically with these lines:
$post_id = $post->ID;
$thumbnail_id = get_post_thumbnail_id( $post_id );
$thumbnail_image = wp_get_attachment_image_src( $thumbnail_id,$thumbnail_size );
The problem is that I couldn't find the way to send to the specific post ids to the function above, since the main $wp_query->posts; retrieves the page id instead of the post requested by the query_posts method.
The loops present the correct posts but when I echo the post->ID it shows the page id.
My query is:
global $wp_query;
// concatenate the query
$args = 'cat='.$idCategory.'&tag=comparativas';
query_posts( $args );
$posts = $wp_query->posts;
$current_id = get_the_ID(); //-> this returns the page id
If you could please tell me how to overwrite the global $wp_query; so the template can handle the corresponding ids for the list of post It would be great.
Any clue?
Best,
Juan
You could use, setup_postdata($post)
Then get_the_ID() works again :)
It doesn't work just because you aren't looping through them. You can do it many ways.
The 2 more common are the following:
overwrite the query with query_posts
$args = array('cat' => $idCategory,'tag' => 'comparativas');
query_posts($args);
if(have_posts()){
while(have_posts()){
the_post();
$current_id = get_the_ID(); // this return what you want now
the_title(); // this works as expected
}
}
wp_reset_query(); // get previous query back
get an array of WP_Post objects and setup_postdata or simply loop through them
$args = array('cat' => $idCategory,'tag' => 'comparativas');
$posts_i_want = get_posts($args);
foreach( $posts_i_want as $post ){
setup_postdata($post);
$current_id = get_the_ID(); // this return what you want now
the_title(); // this works as expected
}
wp_reset_postdata(); // get previous postdata back
I personally prefer the first in most cases
Cheers
I have a page when I want multiple comment_template, so I want each of my comment_template to have different type so I see different comments in those 2.
My get_comments look like this
$args = array(
'post_id' => get_the_ID(),
'orderby' => 'comment_date',
'order' => 'ASC',
'type' => 'tips'
);
$comments = get_comments($args);
The problem is when I post the comment, I didn't find how to assign this post type to the comment, so I am not able to show them up with get_comments()
I tried to add my comment form like this but that still dosen't work
$comment_args = array(
'type' => 'tips'
);
comment_form($comment_args);
There are several steps to saving a custom comment type which, by the way, is technically not supported in WordPress as is a custom post type.
A. WordPress has 3 types of comments: ping, trackback, and blank for regular comment.
B. Using this premise of WP allowing a blank field to associate for "normal comment", we can now insert our new CCT using any name we want.
C. You must use comment_form_defaults in a function to define an array outside the comments form and pass it as a parameter. (Otherwise you can alter the $arguments in comment_form($args) directly on callout.)
add_filter('comment_form_defaults', 'wpse33967503_defaults_comments_positive');
function wpse33967503_defaults_comments_positive($default)
{
global $comment_id;
$actnonce = 'action_tips';
$action = empty( $_REQUEST['action_tips'] )
? false : $_REQUEST['action_tips'];
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $action != false )
{
if( isset( $_POST['custom_tips'] ) ){
$ctype = $_POST['custom_tips'];
$default['fields']['comment_type'] = '<p class="comment-form-tips '. $ctype .'">
<input type="hidden" name="action_tips" value="'. $actnonce .'"
id="action_tips" />
<input type="hidden" name="comment_type" value="'. $ctype .'"
id="comment_type" /></p>';
}
}
return $default;
}
D. Now we can inject this input into the comment form, but validation is always a good thing.
add_filter( 'preprocess_comment', 'wpse33967503_preprocess_comment_handler', 12, 1 );
function wpse33967503_preprocess_comment_handler( $commentdata )
{
if( ( isset( $_POST['comment_type'] ) ) && ( $_POST['comment_type'] != '') ) {
$commentdata['comment_type'] = wp_filter_nohtml_kses( $_POST['comment_type'] );
}
return $commentdata;
}
Feel free to add messages or even a admin validation... I am just checking that it gets posted.
Your CCT is now part of the commentdata (not comment_meta). this is the regular comment data parameters that are inherit of WP.
Here are the default comment data sets in wp_comments table. Technically you could custom type just about any of these.
comment_ID: ID for the comment
comment_post_ID: The post for which the comment is posted
comment_author: Comment author name
comment_author_email: Author name for the comment
comment_author_url: Author url
comment_author_IP: Author IP address
comment_date: Comment Date
comment_date_gmt: GMT time of the comment
comment_content: Contents of the comment (comment body)
comment_karma: Is there default karma calculation? Maybe rating?
comment_approved: Flag for the comment status.
comment_agent:
comment_type: functionality can be used like custom post type
comment_parent: For threaded comment
user_id: user id if comment author is a registered user of the site.
All my post has a custom field created_by_alias, I am basically use the value in place of whereever author is used. I have customized the entry-meta function to display this value in plain text, I have also learned to build a query:
$query = new WP_Query( array('meta_key' => 'created_by_alias',
'meta_value' => 'somevalue' ));
Now for the final part: how to create a link for this piece of text, that brings a listing of all posts by this alias?
Do I need to write a template file or is there a quick way to reuse whatever code that generates links to a category, tag listing?
Not sure what you are looking for but seems to me that you want to show a link for the author posts in a specific page, if so, then you can use (using your query)
$query = new WP_Query( array('meta_key'=>'created_by_alias', 'meta_value'=>'somevalue' ));
If $query returns result and you have the id then you can use
echo get_author_posts_url( $query->posts[0]->ID ); // the `ID` from first row
Or for multiple rows, you can loop like
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
echo get_author_posts_url( get_the_ID() ) . '<br />';
}
}
Once this link is clicked, then posts will be retrieved and will be shown according to your page hierarchy, if no author template was found then finally the inedx.php will be used.
Also, if you need a custom template and want to query
$posts = get_posts(array($ID);
Now, to get the id you an use
get_user_by( $field, $value ); // 'id', 'slug', 'email', or 'login'
So, if you have the login name of the user, for example admin then you can use
$id = get_user_by( 'login', 'admin' )->ID; // php 5+
// or
$user = get_user_by( 'login', 'admin' );
$id = $user->ID;
Then get the link and echo it, whereever you want
echo get_author_posts_url( $id );