• Resolved yst14

    (@yst14)


    Hi

    I have a repeating color selector and I would like to be able to provide my users with premade swatch panels (groups of 3-5 colours).

    I would like to do this by providing them with an importable JSON file in the correct format. If I use an exported file it overwrites what the user already has. Is there a way to not overwrite what the user has but update it to include the new options?

    For example if the user has 3 colors added and they import my file it would ADD 5 colors to make the total 8 colors.

    thanks
    Ben

    https://wordpress.org/plugins/admin-page-framework/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author miunosoft

    (@miunosoft)

    Use the import_{...} filter hook.

    Try adding this method in your posted example plugin.

    public function import_noou_swatches_options( $aImportedData, $aStoredData ) {
    
        $_aDefaultColors = array(
            '#dd3333',
            '#1e73be',
            '#8224e3',
        );
        $_aImportedColors = $this->oUtil->getElementAsArray(
            $aImportedData,
            array( 'repeatable_tabbed_section', 'my_color' )
        );
        $aImportedData[ 'repeatable_tabbed_section'][ 'my_color' ] = array_merge( $_aDefaultColors, $_aImportedColors );
    
        return $aImportedData;
    
    }
    Thread Starter yst14

    (@yst14)

    ADded that in however now when exporting I get a JSON file but when importing it says:

    The uploaded file type is not supported: application/txt

    So I am assuming the mine type for txt can be added? I’m just curious why it will have changed from json to txt?

    Thanks!

    Thread Starter yst14

    (@yst14)

    So changing the mine type to txt from json works! the only thing is that if the colour already exists it still adds it in.

    This is a minor detail but would be nice to avoid if possible.

    Plugin Author miunosoft

    (@miunosoft)

    Just modify the code I posted by adding a condition which checks if the color array is empty or not. If empty, do not merge.

    As for changing the mime type, it should not have to be set. If you exported the data as text, that might be the case though. Otherwise, the default export format is json and you should not be getting such an error. So double-check if you are not modifying anything for the export file format.

    Thread Starter yst14

    (@yst14)

    Hi Again,

    Something I have just noticed that isn’t working as expected.

    The code you sent adds an array of default colours which are coded into the import process.

    I do not want to have these default colours. That is fine I can remove that from the code you sent.

    What I am trying to avoid is overwriting the colours in the system already. For example:

    Saved Colours:
    #fff
    #000
    #333

    Import File Contains:
    #dedede
    #999
    #666

    Final Saved Colours should be:
    #fff
    #000
    #333
    #dedede
    #999
    #666

    Is this possible as the current code doesnt do this?

    Thanks
    Ben

    Plugin Author miunosoft

    (@miunosoft)

    Hi,

    The second parameter receives the stored data. So you can extract the options form it. Then replace the extracted values with the default values in that example code.

    You already know the structure of the options array and how to retrieve values from a multi-dimensional array.

    So give it a try. I believe you can do it.

    Post code if you get stuck.

    Thread Starter yst14

    (@yst14)

    Ahh I’m so silly! yes i didn’t think about calling my current array rather than the default values! Got it working now. Thank you very much!

    Plugin Author miunosoft

    (@miunosoft)

    Glad to help!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘When importing do not overwrite certain fields but update them’ is closed to new replies.