0

I am trying to prepend my a php template in wordpress into my wordpress login form.

They only way I can edit markup in the body of wordpess login is to prepend via jquery.

This is my php template load: <?php load_template_part('menu'); ?>

This is my load template filter that filters get_template_part(); ...

// LOAD TEMPLATE PART
function load_template_part($template_name, $part_name=null) {
    ob_start();
    get_template_part($template_name, $part_name);
    $var = preg_replace('/\s\s+/', "", ob_get_contents());
    ob_end_clean();
    return $var;
}

In this filter I am removing all spaces between elements using this... preg_replace('/\s\s+/', "", ob_get_contents());


I am then trying to append this template part using this jquery...

var menu = '<?php echo load_template_part('menu'); ?>';

$("body").prepend(menu);

But it gets a javascript syntax error because of line breaks in my php load_template_part string.

Can any one please help me either remove line breaks from the php string or suggest another method of getting my php block inside my body safely.

Outputted source: https://gist.github.com/jcranny/5363379

Thanks Josh

Joshc
  • 3,825
  • 16
  • 79
  • 121

2 Answers2

2

What if you simply replace all the linebreaks with spaces?

var menu = '<?php echo preg_replace('/[\r\n]+/',' ',load_template_part('menu')); ?>';

This may work more reliably, although I haven't tested it (borrowed from this answer):

var menu = '<?php echo json_encode(load_template_part('menu')); ?>';
Community
  • 1
  • 1
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
  • I don't think line breaks are this issue, I think the html returned by load_template_part contains literal single quotes. Since the PHP is parsed before the page is loaded, load_template_part() would end up simply being a literal string assigned to the "menu" variable. – Andrew Senner Apr 11 '13 at 13:34
  • @Drewdiddy611 OP was very specific that line breaks were his issue, although single quotes SHOULD also be a concern. – Blazemonger Apr 11 '13 at 13:34
  • Just noticed the comment mentioning there were not single quotes returned.. My apologies :) – Andrew Senner Apr 11 '13 at 13:37
  • @Blazemonger Thank you - this fixed the issue! Now my js string is one long line with no js OP errors from line breaks. Thank you. – Joshc Apr 11 '13 at 14:01
  • Don't forget [this question](http://stackoverflow.com/questions/6269188/how-to-escape-only-single-quotes) if and when you need to escape single quotes in that line. – Blazemonger Apr 11 '13 at 14:52
0

I've run into this issue before, you're going to have problems if that "template" contains single quotes. The work around I've used is to base64_encode the html that is returned from

load_template_part()

Then grab the base64_decode libraries from phpjs.org. This will ensure the string doesn't contain illegal characters that could cause a JS syntax error.

For example:

// With Base64 object from phpjs.org
var menu = Base64.decode('<?php echo base64_encode(load_template_part('menu')); ?>');

Give that a shot, always works like a charm for me.

Andrew Senner
  • 2,479
  • 1
  • 18
  • 24