Skip to content

Instantly share code, notes, and snippets.

@tomjn
Created May 6, 2015 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomjn/d63acb2be62e777d6110 to your computer and use it in GitHub Desktop.
Save tomjn/d63acb2be62e777d6110 to your computer and use it in GitHub Desktop.
<?php
$code = '';
if ( isset( $instance[ 'code' ] ) ) {
$code = $instance[ 'code' ];
// catch PHP tags
if ( strpos( $code, '<?' ) !== false ) {
echo '<div class="error"><p>Embed Code error: You can\'t include PHP code here, it will not work, and will be shown on the frontend. Please remove the PHP code and contact a developer. PHP code is surrounded by &lt;?php and ?&gt;</p></div>';
}
$dom = new \DOMDocument();
libxml_use_internal_errors( true );
$dom->loadHtml( $code );
$errors = libxml_get_errors();
foreach ( $errors as $error ) {
$return = '<strong>Issue found in the embed code:</strong><br>';
switch ( $error->level ) {
case LIBXML_ERR_WARNING:
$return .= "Warning $error->code: ";
break;
case LIBXML_ERR_ERROR:
$return .= "Error $error->code: ";
break;
case LIBXML_ERR_FATAL:
$return .= "Fatal Error $error->code: ";
break;
}
$return .= trim( $error->message ) .
"<br> Line: $error->line" .
", Column: $error->column";
echo '<div class="error"><p>'.wp_kses_post( $return ).'</p></div>';
}
libxml_clear_errors();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment