Last active
May 17, 2019 21:54
-
-
Save nathanieltubb/00c5bc5d847de8ae682663fb92c49520 to your computer and use it in GitHub Desktop.
WordPress Link Map CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var allLinks = document.links,links = [];for (var i=0; i<allLinks.length; i++) {var link = allLinks[i];links.push('"'+link.text+'"'+','+link.href);}document.write(links.join('<br>')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Placing this single-use script in the WP installation and running from the browser | |
* will output the content and taxonomy links in CSV format for purposes like creating | |
* a redirect map. | |
*/ | |
// LOAD IN WP INSTANCE | |
require_once '../wp-load.php'; | |
// SOMEWHERE TO STORE THE TITLE-LINK PAIRS | |
$links = []; | |
//-- GET POSTS -- | |
$all_posts = new WP_Query([ | |
'post_type' => ['post', 'page', 'product', 'musiklus_contributor'], | |
'posts_per_page' => -1, | |
'fields' => 'ids' | |
]); | |
if($all_posts->found_posts > 0) { | |
foreach($all_posts->posts as $pid) { | |
$links[] = [ | |
'title' => get_the_title($pid), | |
'link' => get_permalink($pid) | |
]; | |
} | |
} | |
//-- GET TAXONOMIES -- | |
$taxonomies = get_taxonomies(['public' => true], 'names'); | |
$taxonomies_names = []; | |
foreach ($taxonomies as $taxonomy_name) { | |
$taxonomies_names[] = $taxonomy_name; | |
} | |
$terms = get_terms([ | |
'taxomony' => $taxonomies_names, | |
'hide_empty' => false | |
] | |
); | |
foreach ($terms as $term) { | |
$links[] = [ | |
'title' => $term->taxonomy . ' --- ' . $term->name, | |
'link' => get_term_link($term->term_id) | |
]; | |
} | |
//--- CSV FUNCTION -- | |
function str_putcsv($data) | |
{ | |
# Generate CSV data from array | |
$fh = fopen('php://temp', 'rw'); # don't create a file, attempt | |
# to use memory instead | |
# write out the headers | |
fputcsv($fh, array_keys(current($data))); | |
# write out the data | |
foreach ($data as $row) { | |
fputcsv($fh, $row); | |
} | |
rewind($fh); | |
$csv = stream_get_contents($fh); | |
fclose($fh); | |
return $csv; | |
} | |
//-- OUTPUT CSV -- | |
echo str_replace("\n", "<br>", str_putcsv($links)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment