Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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