亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶有腳本的 Wordpress 插件開發問題

帶有腳本的 Wordpress 插件開發問題

PHP
慕田峪9158850 2022-10-09 17:37:50
您好我是第一次創建插件,我正在使用我的秒表代碼已經內置 PHP 但我的插件無法正常工作,我認為我的腳本沒有按照插件要求正確編寫,如果理解請幫助我.這是代碼:主插件PHP文件 <?php/*Plugin Name: MyStopwatchDescription: Adds a stopwatch to websiteVersion: 1.0.0Author: Samina*/// Exit if acessed directlyif(!defined('ABSPATH')){    exit;}require_once(plugin_dir_path(__FILE__).'/includes/stopwatchscripts.php');function my_stopwatch_function(){    return '<p id="output"></p><div id="controls">  <button id="strtpause" onclick="strtpause()" class="stopwatchbutton">Start</button>   <button id="reset" onclick="reset()" class="stopwatchbutton">Reset</button></div>';}add_shortcode('mystopwatch','my_stopwatch_function'); add_action('wp_enqueue_scripts','my_stopwatch_function');?>腳本文件:   <?php    ob_start();    //Add Scripts    function stopw_add_scripts(){    //Add Main CSS    wp_enqueue_style('stopw-main-style',plugins_url(). '/mystopwatch/css/style.css');    //Add Main JS     wp_enqueue_script('stopw-main-script',plugins_url(). '/mystopwatch/js/main.js');       }add_action('wp_enqueue_scripts','stopw_add_scripts');?>main.js 文件var time=0;    var running=0;    function strtpause () {        if(running==0){            running=1;            increment();            document.getElementById("strtpause").innerHTML="Pause"        }        else{            running=0;            document.getElementById("strtpause").innerHTML="Resume"        }    }    function reset(){    running=0;    time=0;    document.getElementById("strtpause").innerHTML="Start"    document.getElementById("output").innerHTML="00:00:00"    }    function increment(){        if(running==1){        setTimeout(function(){         time++;         var mins=Math.floor(time/10/60);         var secs=Math.floor(time/10);         var teths=time%10;         if(mins<10){            mins="0"+mins;         }         if(secs<10){            secs="0"+secs;         }         document.getElementById("output").innerHTML=mins+":"+secs+":"+teths;         increment();        },100);    }    }
查看完整描述

3 回答

?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

完整的工作代碼和測試。

http://img1.sycdn.imooc.com//6342968b0001517704550378.jpg

<?php

/*

Plugin Name: MyStopwatch

Description: Adds a stopwatch to website

Version: 1.0.0

Author: Samina

*/

// Exit if acessed directly

if(!defined('ABSPATH')){

    exit;

}

ob_start();

// require_once(plugin_dir_path(__FILE__).'/includes/stopwatchscripts.php');

function my_stopwatch_function(){

    return '<p id="output"></p>

<div id="controls">

  <button id="strtpause" onclick="strtpause()" class="stopwatchbutton">Start</button>

   <button id="reset" onclick="reset()" class="stopwatchbutton">Reset</button>

</div>';

}

add_shortcode('mystopwatch','my_stopwatch_function'); 

add_action('wp_enqueue_scripts','my_stopwatch_function');



//Add Scripts

function stopw_add_scripts(){

//Add Main CSS

wp_enqueue_style('stopw-main-style',plugins_url(). '/mystopwatch/css/style.css');

//Add Main JS

 wp_enqueue_script('stopw-main-script',plugins_url(). '/mystopwatch/js/main.js');   

}


add_action('wp_enqueue_scripts','stopw_add_scripts');

?>


查看完整回答
反對 回復 2022-10-09
?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

 <?php

/*

Plugin Name: MyStopwatch

Description: Adds a stopwatch to website

Version: 1.0.0

Author: Samina

*/

// Exit if acessed directly

if(!defined('ABSPATH')){

    exit;

}

require_once(plugin_dir_path(__FILE__).'/includes/stopwatchscripts.php');

function my_stopwatch_function(){


ob_start(); //start output buffering

echo '<p id="output"></p>

<div id="controls">

  <button id="strtpause" onclick="strtpause()" class="stopwatchbutton">Start</button>

   <button id="reset" onclick="reset()" class="stopwatchbutton">Reset</button>

</div>';



//getting content after buffering

$content = ob_get_contents();

ob_end_clean();

return $content;

}

add_shortcode('mystopwatch','my_stopwatch_function'); 

add_action('wp_enqueue_scripts','my_stopwatch_function');

?>

如果您在將標題加載到頁面之前在 php 文件上返回了大字符串,則它顯示在管理端“此插件生成了 X 個字符的意外輸出”


您可以將這些返回的字符串添加到輸出緩沖以防止出現此錯誤。


查看完整回答
反對 回復 2022-10-09
?
四季花海

TA貢獻1811條經驗 獲得超5個贊

您需要更新您的代碼,例如(我已經測試過)-


add_shortcode('mystopwatch','my_stopwatch_function'); 

add_action('wp_footer','MyStopwatch_scripts'); 


function my_stopwatch_function($watch_html) { 


    ob_start(); ?>


    <p id="output"></p>

    <div id="controls">

      <button id="strtpause" onclick="strtpause()" class="stopwatchbutton">Start</button>

       <button id="reset" onclick="reset()" class="stopwatchbutton">Reset</button>

    </div> <?php


    $watch_html = ob_get_contents();

    ob_clean();


    return $watch_html;


}


function MyStopwatch_scripts() { ?>


    <script>

    var time=0;

    var running=0;


    function strtpause () {

        if(running==0){

            running=1;

            increment();

            document.getElementById("strtpause").innerHTML="Pause"


        }

        else{

            running=0;

            document.getElementById("strtpause").innerHTML="Resume"

        }

    }

    function reset(){

    running=0;

    time=0;

    document.getElementById("strtpause").innerHTML="Start"

    document.getElementById("output").innerHTML="00:00:00"

    }

    function increment(){

        if(running==1){

        setTimeout(function(){

         time++;

         var mins=Math.floor(time/10/60);

         var secs=Math.floor(time/10);

         var teths=time%10;

         if(mins<10){

            mins="0"+mins;

         }

         if(secs<10){

            secs="0"+secs;

         }

         document.getElementById("output").innerHTML=mins+":"+secs+":"+teths;

         increment();



        },100);

    }

    }


     </script>

     <?php


}


如果您創建一個簡碼,您應該使用 ob_start() 和 ob_clean() 和 js 腳本,通常與 wp_footer 掛鉤


查看完整回答
反對 回復 2022-10-09
  • 3 回答
  • 0 關注
  • 138 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號