Wainsworld Wainsworld - 6 months ago 36
PHP Question

Echo wordpress page title into onsubmit

I have a single contact form the runs through the footer of every page on my site.

I have got the page title to echo out using

but it is appearing before the form rather than in the string.

$html .= sprintf( '<form onsubmit="ga(\'send\', \'event\', \'newsletter\', \'submitted\', \'' . wp_title('') . '\')" %s>', $atts ) . "\n";

It appears like this:

enter image description here


From documentation:

Your code

wp_title( string $sep = '&raquo;', bool $display = true, string $seplocation = '' )

secod parameter determines if the title will be echoed or returned, in Your case you should set $display parameter to false.


$html .= sprintf( '<form onsubmit="ga(\'send\', \'event\', \'newsletter\', \'submitted\', \'' . wp_title('', false) . '\')" %s>', $atts ) . "\n";