• Resolved Alon Goren

    (@alongoren)


    Thanks everyone…I think this should be simpler than I’m making it, but I’m a total n00b trying to figure this out. Essentially I have a return function:

    function cryptoexchange_function(){
        return'<script>
        const config = {
          sourceAssetAddress: null,
          sourceAmountDecimal: null,
          destinationAssetAddress: null,
          destinationAmountDecimal: null,
          apiKey: "5a8d0a24-7cce-4b3e-9203-1d88034dd64e",
          partnerContractAddress: "XXXXXXXXXXXXXXXXXXXXXXXXXX",
        };
        const nodeId = "totle-widget";
        !function(){const t=document.createElement("script");t.type="text/javascript";const e=()=>{TotleWidget.default.run(config,document.getElementById(nodeId))};t.readyState?t.onreadystatechange=function(){"loaded"!=t.readyState&&"complete"!=t.readyState||(t.onreadystatechange=null,e())}:t.onload=function(){e()},t.src="https://widget.totle.com/latest/dist.js",document.getElementsByTagName("head")[0].appendChild(t)}();
    </script><p><center><small><b>Swap your tokens below to get the best prices across all decentralized crypto exchanges.</b></small></center></p><div id="totle-widget"></div><p></p></html>';
    }

    On the line below, I’d like the partnerContractAddress value to be an option I created in the admin called custom_contract_address OR a default value. Any tips or ideas?

    partnerContractAddress: “XXXXXXXXXXXXXXXXXXXXXXXXXX”,

    Every way I’ve researched breaks the function…

    • This topic was modified 6 years, 1 month ago by Alon Goren.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Your script is including const config = { which is where it should be using something like https://developer.ww.wp.xz.cn/reference/functions/wp_localize_script/ for this same thing. Of course, that function is for use with a script that is enqueued. The advantage of enqueuing is that it can make sure it is output only once and in the order needed by dependencies (which are also output if listed). The HTML portion should be output by a shortcode.

    If you don’t want to change it to do things the WordPress way, though, at least assign the script to a variable, in pieces, so that you can put the values such as the result of get_option in there, and then return the variable. The get_option function has two parameters; the second one is the default value to use when the option isn’t set. https://developer.ww.wp.xz.cn/reference/functions/get_option/

    Thread Starter Alon Goren

    (@alongoren)

    Thanks, Joy!!! I appreciate the support. I read what you sent, played around a bit and ended up doing this (in case anyone else in the future finds this):

    
    function cryptoexchange_function(){
        $contract_address = get_option('custom_contract_address') ?? '0x5180d531C7113C2F7Dcbfc78c8462c6b9b45758f';
        return '<script>
        const config = {
          sourceAssetAddress: null,
          sourceAmountDecimal: null,
          destinationAssetAddress: null,
          destinationAmountDecimal: null,
          apiKey: "5a8d0a24-7cce-4b3e-9203-1d88034dd64e",
          partnerContractAddress: "'. $contract_address .'",
        };
        const nodeId = "totle-widget";
        !function(){const t=document.createElement("script");t.type="text/javascript";const e=()=>{TotleWidget.default.run(config,document.getElementById(nodeId))};t.readyState?t.onreadystatechange=function(){"loaded"!=t.readyState&&"complete"!=t.readyState||(t.onreadystatechange=null,e())}:t.onload=function(){e()},t.src="https://widget.totle.com/latest/dist.js",document.getElementsByTagName("head")[0].appendChild(t)}();                                                                                                                                                      
    </script><p><center><small><b>Swap your tokens below to get the best prices across all decentralized crypto exchanges.</b></small></center></p><div id="totle-widget"></div><p></p></html>';  
    }   
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘get option inside of a return function?’ is closed to new replies.