{"id":2942,"date":"2023-03-21T13:59:01","date_gmt":"2023-03-21T12:59:01","guid":{"rendered":"https:\/\/www.saskialund.de\/?p=2942"},"modified":"2023-11-21T12:24:38","modified_gmt":"2023-11-21T11:24:38","slug":"write-translatable-php-functions-for-wordpress","status":"publish","type":"post","link":"https:\/\/isla-stud.io\/en\/ratgeber\/uebersetzbare-php-funktionen-fuer-wordpress-schreiben\/","title":{"rendered":"Writing translatable PHP functions for WordPress"},"content":{"rendered":"<p>Excerpt from the <a href=\"https:\/\/developer.wordpress.org\/apis\/security\/escaping\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">WordPress developer manual<\/a>:<\/p>\n\n\n\n<p>Under <em>Escaping Output <\/em>is the safeguarding of output data by removing unwanted data\/characters, such as incorrect HTML or script tags. This mechanism helps to ensure that the data is secured for the end user before rendering.<\/p>\n\n\n\n<p>WordPress contains numerous help functions that you can use for the most common scenarios.<\/p>\n\n\n\n<p>Pay close attention to what each function does, as some remove HTML while others allow it. You should use the most appropriate function for the content and context you want to output. The escape function should always be used during the output (php echo), not before.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Escaping functions at a glance<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">esc_attr()<\/code><\/pre>\n\n\n\n<p>Is used for everything else that is output in the attribute of an HTML element. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">esc_html()<\/code><\/pre>\n\n\n\n<p>Is always used when an HTML element includes a section with data that is displayed (removes html).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">esc_js()<\/code><\/pre>\n\n\n\n<p>To be used for inline javascript.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">esc_textarea()<\/code><\/pre>\n\n\n\n<p> For encoding text for use in a textarea element.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">esc_url()<\/code><\/pre>\n\n\n\n<p> Used for all URLs, including those in the src and href attributes of an HTML element.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">esc_url_raw()<\/code><\/pre>\n\n\n\n<p>Used when a URL is stored in the database or in other cases where unencoded URLs are required.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">wp_kses()<\/code><\/pre>\n\n\n\n<p>Use for all untrusted HTML elements (post text, comment text, etc.)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">wp_kses_post()<\/code><\/pre>\n\n\n\n<p>Alternative version of wp_kses() that automatically allows all HTML content that is allowed in posts.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">wp_kses_data()<\/code><\/pre>\n\n\n\n<p>Alternative version of wp_kses(), which only allows the HTML code permitted in post comments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ensure multilingualism when escaping<\/h2>\n\n\n\n<p>If the output is carried out as in the following example, we ensure that<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The sentences remain intact (no sentence breaks).<\/li>\n\n\n\n<li>correct separation is ensured.<\/li>\n\n\n\n<li>the option of arranging contact and e-mail links (or something similar) differently within the translated sentence.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:14px\"><code lang=\"php\" class=\"language-php\">\/\/ Example url (could have come from an insecure user input via a form, for example).\n$contact_url = &#039;https:\/\/www.example.com\/contact\/&#039;;\n\/\/ escaping $contact_url\n$contact_url = esc_url( $contact_url );\n\n\/\/ Example e-mail address (could have come from an insecure user input via a form, for example).\n$contact_email = &#039;info@mydomain.com&#039;;\n\/\/ escaping, sanitizing &amp;amp; hiding of $contact_email.\n\/\/ Yes, you should still sanitize and escape the email address even if you use the antispambot() function\n$contact_email = esc_url( sprintf( &#039;mailto:%s&#039;, antispambot( sanitize_email( $contact_email ) ) ), array( &#039;mailto&#039; ) );\n\nesc_html_e( &#039;Dear guest, we were unable to find any details regarding your request.&#039;, &#039;text-domain&#039; );\necho &quot;&lt;br&gt;&lt;br&gt;&quot;;\n\nprintf(\n    esc_html__( &#039;Please contact us through our %1$s or via %2$s.&#039;, &#039;text-domain&#039; ),\n    sprintf(\n        &#039;&lt;a href=&quot;\/en\/s-2\/&quot;&gt;%s&lt;\/a&gt;&#039;,\n        $contact_url,\n        esc_html__( &#039;Contact Page&#039;, &#039;text-domain&#039; )\n        ),\n    sprintf(\n        &#039;&lt;a href=&quot;\/en\/s-2\/&quot;&gt;%s&lt;\/a&gt;&#039;,\n        $contact_email,\n        esc_html__( &#039;Email&#039;, &#039;text-domain&#039; )\n        )\n    );<\/code><\/pre>\n\n\n\n<p>This gives the translator two complete sentences and two individual words to translate. A translator therefore only has to take care of the following simple lines (while the CODE takes care of the rest):<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:14px\"><code lang=\"php\" class=\"language-php\">esc_html_e( 'Dear guest, we were unable to find any details regarding your request.', 'text-domain' );\n\/\/ ...\nesc_html__( 'Please contact us through our %1$s or via %2$s', 'text-domain' )\n\/\/ ...\nesc_html__( 'Contact Page', 'text-domain' )\n\/\/ ...\nesc_html__( 'Email', 'text-domain' )<\/code><\/pre>\n\n\n\n<p class=\"has-small-font-size\">Further information can be found here: <a href=\"https:\/\/developer.wordpress.org\/themes\/functionality\/internationalization\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Multilingualism for WordPress themes (eng)<\/a> and <a href=\"https:\/\/developer.wordpress.org\/plugins\/internationalization\/how-to-internationalize-your-plugin\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Multilingualism for plugins (eng)<\/a><\/p>\n\n\n\n<p>If you have any questions, please feel free to use the comment function at the bottom of this page.<\/p>","protected":false},"excerpt":{"rendered":"<p>Here you will find useful PHP snippets to output texts translatable in WordPress and to realize multilingual themes and plugins for WordPress.<\/p>","protected":false},"author":1,"featured_media":2956,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[13,644,20],"tags":[],"dipi_cpt_category":[],"class_list":["post-2942","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ratgeber","category-code-snippets","category-tipps-tricks"],"acf":[],"_links":{"self":[{"href":"https:\/\/isla-stud.io\/en\/wp-json\/wp\/v2\/posts\/2942","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/isla-stud.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/isla-stud.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/isla-stud.io\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/isla-stud.io\/en\/wp-json\/wp\/v2\/comments?post=2942"}],"version-history":[{"count":2,"href":"https:\/\/isla-stud.io\/en\/wp-json\/wp\/v2\/posts\/2942\/revisions"}],"predecessor-version":[{"id":3109,"href":"https:\/\/isla-stud.io\/en\/wp-json\/wp\/v2\/posts\/2942\/revisions\/3109"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/isla-stud.io\/en\/wp-json\/wp\/v2\/media\/2956"}],"wp:attachment":[{"href":"https:\/\/isla-stud.io\/en\/wp-json\/wp\/v2\/media?parent=2942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/isla-stud.io\/en\/wp-json\/wp\/v2\/categories?post=2942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/isla-stud.io\/en\/wp-json\/wp\/v2\/tags?post=2942"},{"taxonomy":"dipi_cpt_category","embeddable":true,"href":"https:\/\/isla-stud.io\/en\/wp-json\/wp\/v2\/dipi_cpt_category?post=2942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}