1

I would like to replace part of a string in a plugin I am using that inserts a paypal button.

This string is in this function. I have commented out a lot of the excess and showing which parts I would like to filter:

add_shortcode('paypal', 'paypal_options');
function paypal_options($atts) {
  $atts = shortcode_atts(array('name' => 'Example Name','price' => '0.00','size' => '','align' => ''), $atts);
  $options = get_option('paypal_settingsoptions');
  foreach ($options as $k => $v ) { $value[$k] = $v; }

  $output = "";
//commented out excess code
  $output .= "<div $alignment>";
  $output .= "<form target='$target' action='https://www.$path.com/cgi-bin/webscr' method='post'>";
//commented out excess code
  $output .= "<input style='border: none;' class='paypalbuttonimage' type='image' src='$img' border='0' name='submit' alt='Make your payments with PayPal. It is free, secure, effective.'>";

  return $output;
}

I want to replace this specific string:

<input style='border: none;' class='paypalbuttonimage' type='image' src='$img' border='0' name='submit' alt='Make your payments with PayPal. It is free, secure, effective.'>

with this:

<input style='border: none;' class='paypalbuttonimage' type='submit' value='Buy Now' border='0' name='submit' alt='Make your payments with PayPal. It is free, secure, effective.'>

Changing it from an input type='image' to input type='submit'

I am trying this, but it doesn't seem to catch the operator:

function replace_ppbutton($output) {
  $og = "<input style='border: none;' class='paypalbuttonimage' type='image' src='$img' border='0' name='submit' alt='Make your payments with PayPal. It is free, secure, effective.'>";
  $rep = "<input style='border: none;' class='paypalbuttonimage' type='submit' value='Buy Now' border='0' name='submit' alt='Make your payments with PayPal. It is free, secure, effective.'>";
  return str_replace($og, $rep, $output);
}
add_filter('paypal_options', 'replace_ppbutton');
antonanton
  • 581
  • 4
  • 13
  • 30
  • Double check the [manual](https://developer.wordpress.org/reference/functions/add_filter/) for adding filters in the wordperss codex. The first parameter is the tag, not the content that you wish to filter. – Luke Nov 23 '16 at 23:04
  • Hey @Luke, I modified the code but it still doesn't seem to catch my filter… Edited original question – antonanton Nov 27 '16 at 04:03

1 Answers1

0

Old question I know, but I think altering markup with string replacement is probably even more fragile than using regular expressions!

Try using a DOM parser with an XPath query instead; something like this:

<?php
function replace_ppbutton($output) {
    $dom = new DomDocument();
    libxml_use_internal_errors(true);
    $dom->loadHTML($output);
    $xpath = new DomXPath($dom);
    $button = $xpath->query("//input[@class='paypalbuttonimage' and @type='image']")->item(0);
    $button->setAttribute("type", "submit");
    $button->removeAttribute("src");
    return $dom->saveHTML();
}
miken32
  • 42,008
  • 16
  • 111
  • 154