4

I've got a custom module that is sending some emails and I want to put the site name in the subject line, but whenever I look up the site_name in code, it is wrapped in html.

This is the code I use to generate the subject line:

$variables = array( '%site_name' => variable_get('site_name'), '%tags' => $params['tags'], ); $subject = t('%site_name: Verify your Subscription', $variables); error_log("subject=$subject"); 

In the email I received and in the error log, this is what $subject is:

<em class="placeholder">liberalgeek.com</em>: Verify your Subscription 

Why is the site_name wrapped in html?

How can I either strip the html or lookup the site_name in a different way so that it doesn't have the html to start?

2 Answers 2

6

The problem is that you are using bad modifier for the t() function.

$variables = array( '@site_name' => variable_get('site_name'), '@tags' => $params['tags'], ); $subject = t('@site_name: Verify your Subscription', $variables); error_log("subject=$subject"); 

Note the @site_name change. If you use

  • @site_name -- it will be run through check_plain and returns without any theming.
  • %site_name -- check_plain with theme_placeholder.
  • !site_name -- Allows all HTML.

Also note that this is not limited to site name. Whenever you use t() function, you should use above placeholder pattern to ensure CSRF-safe variable display. Do not use t() function for use-entered variables. For an example, t($form_state['values']['name']); is bad!. Real use of t() function is when you use it with your own text and optionally user-entered data.

Good luck!

2
  • 1
    The site_name variable shouldn't be passed through t() either. Multilingual Variables (part of Internationalization) helps to specify and retrieve variables in different languages. Check out this blogpost for more info: hojtsy.hu/blog/2011-feb-25/… Commented Jan 13, 2013 at 22:40
  • thanks for pointing that! OP's use in t() with own text and sitename is correct though. Commented Jan 14, 2013 at 0:37
0

It is in the variable table. You can read it directly from the database. In Drupal 7 this value is of type blob (in Drupal 6 it was longtext). It is serialized so you cannot simply read it with a select query.

I have some PHP code here that shows how to write to it and read from it. http://www.siteconsortium.com/h/p1.php?id=drpl001

Also, if you are using Drupal 7 the link above it shows how read it in the UI (Configuration -> System -> Site Information). Modifying it here works.

Don't forgot to dump the cache though if cache is enabled if you change it in the database.

1
  • The linked page doesn't show code anymore. Commented Mar 17, 2018 at 18:40

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.