* @package WPSEO\Admin\Export
* Class with functionality to export the WP SEO settings.
* Holds the nonce action.
const NONCE_ACTION = 'wpseo_export';
* Holds whether the export was a success.
* Handles the export request.
public function export() {
check_admin_referer( self::NONCE_ACTION );
$this->export_settings();
public function output() {
if ( ! WPSEO_Capability_Utils::current_user_can( 'wpseo_manage_options' ) ) {
esc_html_e( 'You do not have the required rights to export settings.', 'wordpress-seo' );
echo '<p id="wpseo-settings-export-desc">';
/* translators: %1$s expands to Import settings */
'Copy all these settings to another site\'s %1$s tab and click "%1$s" there.',
/* translators: %1$s expands to Yoast SEO */
echo '<label for="wpseo-settings-export" class="yoast-inline-label">' . sprintf( __( 'Your %1$s settings:', 'wordpress-seo' ), 'Yoast SEO' ) . '</label><br />';
echo '<textarea id="wpseo-settings-export" rows="20" cols="100" aria-describedby="wpseo-settings-export-desc">' . esc_textarea( $this->export ) . '</textarea>';
* Exports the current site's WP SEO settings.
private function export_settings() {
foreach ( WPSEO_Options::get_option_names() as $opt_group ) {
$this->write_opt_group( $opt_group );
* Writes the header of the export.
private function export_header() {
/* translators: %1$s expands to Yoast SEO, %2$s expands to Yoast.com */
esc_html__( 'These are settings for the %1$s plugin by %2$s', 'wordpress-seo' ),
$this->write_line( '; ' . $header );
* Writes a line to the export.
* @param string $line Line string.
* @param bool $newline_first Boolean flag whether to prepend with new line.
private function write_line( $line, $newline_first = false ) {
$this->export .= PHP_EOL;
$this->export .= $line . PHP_EOL;
* Writes an entire option group to the export.
* @param string $opt_group Option group name.
private function write_opt_group( $opt_group ) {
$this->write_line( '[' . $opt_group . ']', true );
$options = get_option( $opt_group );
if ( ! is_array( $options ) ) {
foreach ( $options as $key => $elem ) {
if ( is_array( $elem ) ) {
for ( $i = 0; $i < $count; $i++ ) {
$this->write_setting( $key . '[]', $elem[ $i ] );
$this->write_setting( $key, $elem );
* Writes a settings line to the export.
* @param string $key Key string.
* @param string $val Value string.
private function write_setting( $key, $val ) {
if ( is_string( $val ) ) {
$this->write_line( $key . ' = ' . $val );