Skip to content

Commit

Permalink
Fixed the url of files in the images directory
Browse files Browse the repository at this point in the history
  • Loading branch information
litefeel committed Apr 6, 2020
1 parent c0935ac commit 48c031e
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions writing-on-github.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,28 @@ public function ignore_post_meta($meta) {
public function the_content($content) {
$arr = wp_upload_dir();
$baseurl = $arr['baseurl'] . '/writing-on-github';
$basedir = $arr['basedir'] . '/writing-on-github';

$content = preg_replace_callback(
'/(<img [^>]*?src=[\'"])\s*(\/images\/[^\s#]\S+)\s*([\'"][^>]*?>)/',
function($matchs) use ($baseurl) {
$url = $baseurl . $matchs[2];
return "${matchs[1]}$url${matchs[3]}";
'/(<img [^>]*?src=[\'"])\S*?(\/images\/\S+)([\'"].*?>)/',
function($matchs) use ($baseurl, $basedir) {
if (is_file($basedir . $matchs[2])) {
$url = $baseurl . $matchs[2];
return "${matchs[1]}$url${matchs[3]}";
}
return "${matchs[0]}";
},
$content
);

$content = preg_replace_callback(
'/(<a [^>]*?href=[\'"])\s*(\/images\/[^\s#]\S+)\s*([\'"][^>]*?>)/',
function($matchs) use ($baseurl) {
$url = $baseurl . $matchs[2];
return "${matchs[1]}$url${matchs[3]}";
'/(<a [^>]*?href=[\'"])\S*?(\/images\/S+)\s*([\'"].*?>)/',
function($matchs) use ($baseurl, $basedir) {
if (is_file($basedir . $matchs[2])) {
$url = $baseurl . $matchs[2];
return "${matchs[1]}$url${matchs[3]}";
}
return "${matchs[0]}";
},
$content
);
Expand Down

0 comments on commit 48c031e

Please sign in to comment.