Home › Forums › Newsletter Plugin Support › my suggestion for using custom tags in the newsletter text – not a help request
- This topic has 1 reply, 2 voices, and was last updated 2 years, 3 months ago by Stefano.
-
AuthorPosts
-
August 4, 2022 at 5:59 pm #288176AnonymousInactive
hello.
I was asked to import several hundreds of users in a wordpress site and send them their username and password to access an otherwise restricted page, so I thought to use the Newsletter plugin and see if I could modify it to use some custom {tags} in the newsletter text.
for reasons too long to explain here I was asked not to let future users just use the wordpress functionalities that permit them to register themselves, choosing their username and pw etc., and I was also asked not to expose them the wordpress backend interface (I did this with the plugins “Clean Login” and “Custom Control”).
I was given a csv files with the users data (name, surname, email, password etc.). I imported them as wordpress users with the “Import and export users and customers” plugin – everything smooth, then I imported them as newsletter_users with the add-on Advanced Import, everything smooth again. I just added an Extra Field for the initially assigned password (users can change it later on).
I found where, in the code of Newsletter plugin, the tags search and replace was made: the function replace() at row 2005 of the includes/module.php file.
just before the return $text; row I added this:
<code> if ( strpos ( $text, '{assigned_password}' ) !== false ) { $assigned_password = my_function_to_extract_the_assigned_password( $user ) ; $text = str_replace ( '{assigned_password}', $assigned_password, $text ) ; } </code>
my_function_for_assigned_password() is a function I placed in the function.php file of the child theme to search for the $user assigned password in the users csv file, transformed in an array, and return it. something like that:
<code> my_function_to_extract_the_assigned_password( $user ) { $csv = "namesurname;name.surname@example.com;assigned_strong_pw;Name Surname;Name;Surname; ..."; // note: no spaces between "" $arr_users = array(); $arr_csv = explode ( "\n", $csv ); foreach ($arr_csv as $row) { $arr_row = explode(';', $row); $arr_users[ $arr_row[0] ] = array( 'username' => $arr_row[0], 'email' => $arr_row[1], 'password' => $arr_row[2], 'name' => $arr_row[3], 'first_name' => $arr_row[4], 'last_name' => $arr_row[5], ); } $username = strtolower($user->name) . strtolower($user->surname) ; $password = $arr_users[$username]['password']; return $password; } </code>
ok, I know there are a lot of redundancies, and that I would read the entire array each time the function is called, that is, each time the newsletter text is created for the specific user, and that I could have used specific php functions to read a csv file in an array, but I preferred not to leave – even if for a short time – a file with such information in the filesystem, and placing its content in a .php file seemed a good idea.
in this case I just needed to return one string, $password, but returning an array would allow to use more custom tags.
the code to add to the replace() function could then be something like:
<code> if ( function_exists ( developer_function_to_extract_custom_tags ) ) { $arr_my_custom_tags = developer_function_to_extract_custom_tags( $user ) if ( is_array ( $arr_my_custom_tags ) ) { foreach ( $arr_my_custom_tags as $key_custom_tag => $value_custom_tag ) { if ( strpos ( $text, '{' . $key_custom_tag . '}' ) !== false ) { $text = str_replace ( '{' . $key_custom_tag . '}' , $value_custom_tag, $text ) ; } } } }
you just would have to tell the developer that the function MUST be called that way, or another of your choice.
or, maybe better, you could set a custom hook just before the return $text; row:
<code> ... apply_filters ( 'newsletter_replace_custom_tags', $text, $user ) ; return $text; }
and let the developer to hook in his own tag replacement function.
August 5, 2022 at 2:28 pm #288195StefanoKeymasterIf you imported the password as extra field, why not use the {profile_1} tag, assumed the password has been assigned to the profile number 1?
-
AuthorPosts
- You must be logged in to reply to this topic.