tvjpblogs Posted June 3, 2015 Share Posted June 3, 2015 (edited) Hi! I'm trying to display user metadata in the message body of a ninja contact form in a wordpress website. I'm using a php file called ninjacss.php that is being imported by the options of the theme I am using. It's supposed to grab the user metadata, and display the values within the wordpress page the ninja form generates so the information can be auto generated and sent along with the other fields that the user must fill in. I can place simple php in a post or page and it works and shows me what I want, but I can't place php code to my knowledge in a ninja contact form so I decided to see if declaring a special css class that adds content to the message module would be a proper workaround. In theory it works as I can use the css class to add content that is plain text, but when trying to dynamically populate the data using php, it gets a little tricky. The code is as follows: <?php /*** set the content type header ***/ header("Content-type: text/css"); ?> .ninjaphptest:before { content: "<?php global $current_user; get_currentuserinfo(); echo $current_user->display_name . "\n"; echo $current_user->pmpro_baddress1 . " "; echo $current_user->pmpro_baddress2 . "\n"; echo $current_user->pmpro_bcity . ", "; echo $current_user->pmpro_bstate . " "; echo $current_user->pmpro_bzipcode . "\n"; echo $current_user->user_email . "\n"; echo 'Social Security Number: ' . $current_user->SSN. "\n"; ?>" ; } The code above does absolutely nothing. To test if the file is being referenced correctly I changed it to something with a simple variable like below: <?php /*** set the content type header ***/ header("Content-type: text/css"); $current_user = "testing 1 2 testing 1 2"; ?> .ninjaphptest:before { content: "<?php echo $current_user; ?>" ; } This displays the desired testing result of "testing 1 2 testing 1 2" This signifies that the ninjacss.php file is being referenced, php is being recognized in that file through the wordpress setup, and that there is definitely something wrong in the previous code. Looking for answers and/or direction as to how to go about getting this to work. If you're up for this challenge(at least to this php novice) please reply. Any feedback/guidance is greatly appreciated. Thank you! Edited June 3, 2015 by tvjpblogs Quote Link to comment https://forums.phpfreaks.com/topic/296630-php-in-css-to-display-wordpress-user-metadata/ Share on other sites More sharing options...
Muddy_Funster Posted June 4, 2015 Share Posted June 4, 2015 first up, check that your global $current_user has something in it by var_dump() ing it , next, tell us what's happening in the get_currentuserinfo() function that you call in there. Then we can see what's going on. Quote Link to comment https://forums.phpfreaks.com/topic/296630-php-in-css-to-display-wordpress-user-metadata/#findComment-1513148 Share on other sites More sharing options...
maxxd Posted June 4, 2015 Share Posted June 4, 2015 Do none of the Ninja Forms hooks give you the chance to inject the data you want? Using add_filter() in your functions file would at least keep the php together and not force you to try to hack it into the CSS - it seems like the ninja_forms_display_fields hook might be the one you're looking for. Quote Link to comment https://forums.phpfreaks.com/topic/296630-php-in-css-to-display-wordpress-user-metadata/#findComment-1513150 Share on other sites More sharing options...
tvjpblogs Posted June 4, 2015 Author Share Posted June 4, 2015 (edited) Thank you both! Truthfully just learning how to use hooks. My design experience has been more focused on visual design than code, but I can code and interpret code enough to get by. Please forgive if this sounds like an amateur question, but how would I use ninja_forms_display_fields to display user metadata as text within the message body? It looks interesting upon research, but can't figure out how and where to use it. Edited June 4, 2015 by tvjpblogs Quote Link to comment https://forums.phpfreaks.com/topic/296630-php-in-css-to-display-wordpress-user-metadata/#findComment-1513157 Share on other sites More sharing options...
maxxd Posted June 4, 2015 Share Posted June 4, 2015 Your theme has a file called functions.php. If you open that up, you'll find all the hooks that the theme uses, and it's here you'd add the call to ninja_forms_display_fields like so: add_action('ninja_forms_display_fields', 'thisIsMyFunction', 0, 10); function thisIsMyFunction($formID){ //do whatever you need to do, using $formID } Again, I'm not sure this is the hook you need, but it's a place to start. Also, if you're using a downloaded or purchased theme you might want to create a child theme out of it before you start messing with the functions.php file as that file runs your entire site. Quote Link to comment https://forums.phpfreaks.com/topic/296630-php-in-css-to-display-wordpress-user-metadata/#findComment-1513160 Share on other sites More sharing options...
tvjpblogs Posted June 4, 2015 Author Share Posted June 4, 2015 (edited) Many thanks, you are really helping clarify! I've added the call to ninja forms in a child theme functions.php file Not sure what you mean though by //do whatever you need to do, using $formID. Is this where I would place my code, and does $formID need to be defined? add_action('ninja_forms_display_fields', 'thisIsMyFunction', 0, 10); function thisIsMyFunction($formID){ //do whatever you need to do, using $formID global $current_user; get_currentuserinfo(); echo $current_user->display_name . "\n"; echo $current_user->pmpro_baddress1 . " "; echo $current_user->pmpro_baddress2 . "\n"; echo $current_user->pmpro_bcity . ", "; echo $current_user->pmpro_bstate . " "; echo $current_user->pmpro_bzipcode . "\n"; echo $current_user->user_email . "\n"; echo 'Social Security Number: ' . $current_user->SSN. "\n"; ?> }; Edited June 4, 2015 by tvjpblogs Quote Link to comment https://forums.phpfreaks.com/topic/296630-php-in-css-to-display-wordpress-user-metadata/#findComment-1513166 Share on other sites More sharing options...
maxxd Posted June 4, 2015 Share Posted June 4, 2015 $formID is passed in to your function from the ninja_forms_display_fields hook. There may be other parameters passed, but the docs only mentioned this one. Try just printing it to see if it's an ID, or an array, or what it is - you may have to use it to get further data from the database. If you run the code you have above, what's the output? I got the idea that the hook is fired before each field is displayed (not just once), but I could be mistaken about that. Quote Link to comment https://forums.phpfreaks.com/topic/296630-php-in-css-to-display-wordpress-user-metadata/#findComment-1513173 Share on other sites More sharing options...
tvjpblogs Posted June 4, 2015 Author Share Posted June 4, 2015 Running the code above gives an error: Parse error: syntax error, unexpected end of file in /home/cpcportal/public_html/wp-content/themes/life-credit-debt-rn-child-theme/functions.php on line 51 I know I'm making novice mistakes. But hopefully it's very simple to correct. Quote Link to comment https://forums.phpfreaks.com/topic/296630-php-in-css-to-display-wordpress-user-metadata/#findComment-1513174 Share on other sites More sharing options...
tvjpblogs Posted June 4, 2015 Author Share Posted June 4, 2015 (edited) Thank you, thank you, and thank you!!!! I caught the error. had an extra closing tag where none was needed. Changed to code below: add_action('ninja_forms_display_fields', 'thisIsMyFunction', 0, 10); function thisIsMyFunction($formID){ //do whatever you need to do, using $formID global $current_user; get_currentuserinfo(); echo $current_user->display_name . "\n"; echo $current_user->pmpro_baddress1 . " "; echo $current_user->pmpro_baddress2 . "\n"; echo $current_user->pmpro_bcity . ", "; echo $current_user->pmpro_bstate . " "; echo $current_user->pmpro_bzipcode . "\n"; echo $current_user->user_email . "\n"; echo 'Social Security Number: ' . $current_user->SSN. "\n"; }; This works and is displaying the information needed. Just need to format it as it is not honoring the line breaks Edited June 4, 2015 by tvjpblogs Quote Link to comment https://forums.phpfreaks.com/topic/296630-php-in-css-to-display-wordpress-user-metadata/#findComment-1513176 Share on other sites More sharing options...
tvjpblogs Posted June 4, 2015 Author Share Posted June 4, 2015 Okay, so I've fixed line breaks, and all is formatted and populating as it should be, thanks to your assistance. The only thing left, or shall I say the last piece of this puzzle is making this information send along with the form. Do you know if this is possible with another hook to include this data in the email that is sent when the user presses send? Quote Link to comment https://forums.phpfreaks.com/topic/296630-php-in-css-to-display-wordpress-user-metadata/#findComment-1513178 Share on other sites More sharing options...
maxxd Posted June 4, 2015 Share Posted June 4, 2015 I would start here - wish I could be more specific, but I've only used Ninja Forms on one site and I didn't have to interact with it too much on a code level, so I'm not really sure which is going to be the right place to start. Quote Link to comment https://forums.phpfreaks.com/topic/296630-php-in-css-to-display-wordpress-user-metadata/#findComment-1513179 Share on other sites More sharing options...
tvjpblogs Posted June 4, 2015 Author Share Posted June 4, 2015 Thank you so much for your help maxxd! I will go there and see what I can find. Quote Link to comment https://forums.phpfreaks.com/topic/296630-php-in-css-to-display-wordpress-user-metadata/#findComment-1513183 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.