Created
July 15, 2025 18:11
-
-
Save wolffe/7077ae0e90286815a99e828af401537f to your computer and use it in GitHub Desktop.
Create pages hierarchically based on their URL structure
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 | |
/** | |
* Plugin Name: WPPD Importer | |
* Plugin URI: https://www.4property.com/ | |
* Description: Importer. | |
* Version: 0.0.1 | |
* Author: 4Property | |
* Author URI: https://www.4property.com/ | |
* License: GNU General Public License v3 or later | |
* License URI: https://www.gnu.org/licenses/gpl-3.0.html | |
*/ | |
function wppdi_menu_links() { | |
add_menu_page( 'Importer Settings', 'Importer', 'manage_options', 'wppd_importer', 'wppd_importer_build_admin_page', 'dashicons-database-import' ); | |
} | |
add_action( 'admin_menu', 'wppdi_menu_links', 10 ); | |
function wppd_importer_build_admin_page() { | |
$tab = ( filter_has_var( INPUT_GET, 'tab' ) ) ? filter_input( INPUT_GET, 'tab' ) : 'dashboard'; | |
$section = 'admin.php?page=wppd_importer&tab='; | |
?> | |
<div class="wrap"> | |
<h1>Importer</h1> | |
<h2 class="nav-tab-wrapper nav-tab-wrapper-wppd"> | |
<a href="<?php echo $section; ?>dashboard" class="nav-tab <?php echo $tab === 'dashboard' ? 'nav-tab-active' : ''; ?>">Dashboard</a> | |
<a href="<?php echo $section; ?>import" class="nav-tab <?php echo $tab === 'import' ? 'nav-tab-active' : ''; ?>">Import</a> | |
</h2> | |
<?php if ( $tab === 'dashboard' ) { ?> | |
<h3>Importer <code class="codeblock"><?php echo $plugin_version['Version']; ?></code></h3> | |
<?php | |
} elseif ( $tab === 'import' ) { | |
?> | |
<h2>Import</h2> | |
<?php | |
if ( isset( $_POST['save_settings'] ) ) { | |
$wppdi_pages = isset( $_POST['wppdi_pages'] ) ? sanitize_textarea_field( $_POST['wppdi_pages'] ) : ''; | |
$wppdi_pages_to_import = array_map( 'trim', explode( PHP_EOL, $wppdi_pages ) ); | |
$wppdi_pages_to_import = array_unique( array_filter( $wppdi_pages_to_import ) ); | |
foreach ( $wppdi_pages_to_import as $wppdi_page ) { | |
$title = basename( parse_url( $wppdi_page, PHP_URL_PATH ) ); | |
// Split the URL by "/" | |
$url_parts = explode('/', trim(parse_url($wppdi_page, PHP_URL_PATH), '/')); | |
// Determine the parent page ID dynamically | |
$parent_page_id = null; | |
foreach ($url_parts as $part) { | |
if ($part === '') continue; // Skip empty parts | |
$page = get_page_by_path($part, OBJECT, 'page', $parent_page_id); | |
if ($page) { | |
$parent_page_id = $page->ID; | |
} else { | |
// If a part of the URL doesn't correspond to an existing page, break the loop | |
break; | |
} | |
} | |
// Prepare the page data | |
$page_data = array( | |
'post_title' => $title, | |
'post_content' => 'n/a', | |
'post_status' => 'publish', | |
'post_type' => 'page', | |
'post_parent' => $parent_page_id, | |
); | |
$page_id = wp_insert_post( $page_data ); | |
if ( $page_id ) { | |
echo "Page created successfully: $title<br>"; | |
} else { | |
echo "Failed to create page: $title<br>"; | |
} | |
} | |
echo '<div class="updated notice is-dismissible"><p>Settings updated successfully!</p></div>'; | |
} | |
?> | |
<p> | |
<b>Notes:</b><br> | |
Do not add the homepage URL<br> | |
</p> | |
<form method="post"> | |
<table class="form-table"> | |
<tbody> | |
<tr> | |
<th scope="row"><label>Pages</label></th> | |
<td> | |
<p> | |
<textarea class="large-text" rows="24" name="wppdi_pages"></textarea> | |
<br><small>Add one or more page links (one URL per line).</small> | |
</p> | |
</td> | |
</tr> | |
<tr> | |
<th scope="row"><input type="submit" name="save_settings" class="button button-primary" value="Save Changes"></th> | |
<td></td> | |
</tr> | |
</tbody> | |
</table> | |
</form> | |
<?php | |
} | |
?> | |
</div> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment