Jump to content

PHP in CSS to display Wordpress User Metadata


tvjpblogs

Recommended Posts

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 by tvjpblogs
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by tvjpblogs
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by tvjpblogs
Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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 by tvjpblogs
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.