Same ACF on two different pages

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I've made quite an extensive page.php using ACF-repeaters and the flexible content module (1000+ lines of code). The code is getting really spaghetti-like, and I'm not sure how to structure it so it's easier to read and maintain.



Here's a pseudo-code-version of it:



$all_fields = get_fields(); 
foreach( $all_fields as $field ):
if( $field == 'field_1' ):
// Deal with field_1
elseif( $field == 'field_2' ):
// Deal with field_2
elseif( $field == 'field_3' ):
// Deal with field_3
...
... etc.
... etc.
endif;
endforeach;


Now I'm facing a challenge that will require me to structure the code better; I will need another page-template (page-foobar.php), that includes the same ACF-code as shown above.



On the one hand side, then I thought about making this whole thing into a (or several) functions and then sticking it in functions.php. But then I'm polluting the functions.php-file, which would suck.



Ideally, I would do this 'the WordPress way', if there is such a thing for what I'm doing, but I haven't heard of such a thing. So if there isn't one, - then I'd prefer to have a folder in my theme called 'acf-modules', and then a file for each field, containing a function like this:



function field_1( $field_information )
// Deal with $field_information



But should I then make an action in functions.php, requiring each file upon WordPress' init-hook? Is that really the cleanest/best way to do this?










share|improve this question





























    up vote
    1
    down vote

    favorite












    I've made quite an extensive page.php using ACF-repeaters and the flexible content module (1000+ lines of code). The code is getting really spaghetti-like, and I'm not sure how to structure it so it's easier to read and maintain.



    Here's a pseudo-code-version of it:



    $all_fields = get_fields(); 
    foreach( $all_fields as $field ):
    if( $field == 'field_1' ):
    // Deal with field_1
    elseif( $field == 'field_2' ):
    // Deal with field_2
    elseif( $field == 'field_3' ):
    // Deal with field_3
    ...
    ... etc.
    ... etc.
    endif;
    endforeach;


    Now I'm facing a challenge that will require me to structure the code better; I will need another page-template (page-foobar.php), that includes the same ACF-code as shown above.



    On the one hand side, then I thought about making this whole thing into a (or several) functions and then sticking it in functions.php. But then I'm polluting the functions.php-file, which would suck.



    Ideally, I would do this 'the WordPress way', if there is such a thing for what I'm doing, but I haven't heard of such a thing. So if there isn't one, - then I'd prefer to have a folder in my theme called 'acf-modules', and then a file for each field, containing a function like this:



    function field_1( $field_information )
    // Deal with $field_information



    But should I then make an action in functions.php, requiring each file upon WordPress' init-hook? Is that really the cleanest/best way to do this?










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I've made quite an extensive page.php using ACF-repeaters and the flexible content module (1000+ lines of code). The code is getting really spaghetti-like, and I'm not sure how to structure it so it's easier to read and maintain.



      Here's a pseudo-code-version of it:



      $all_fields = get_fields(); 
      foreach( $all_fields as $field ):
      if( $field == 'field_1' ):
      // Deal with field_1
      elseif( $field == 'field_2' ):
      // Deal with field_2
      elseif( $field == 'field_3' ):
      // Deal with field_3
      ...
      ... etc.
      ... etc.
      endif;
      endforeach;


      Now I'm facing a challenge that will require me to structure the code better; I will need another page-template (page-foobar.php), that includes the same ACF-code as shown above.



      On the one hand side, then I thought about making this whole thing into a (or several) functions and then sticking it in functions.php. But then I'm polluting the functions.php-file, which would suck.



      Ideally, I would do this 'the WordPress way', if there is such a thing for what I'm doing, but I haven't heard of such a thing. So if there isn't one, - then I'd prefer to have a folder in my theme called 'acf-modules', and then a file for each field, containing a function like this:



      function field_1( $field_information )
      // Deal with $field_information



      But should I then make an action in functions.php, requiring each file upon WordPress' init-hook? Is that really the cleanest/best way to do this?










      share|improve this question















      I've made quite an extensive page.php using ACF-repeaters and the flexible content module (1000+ lines of code). The code is getting really spaghetti-like, and I'm not sure how to structure it so it's easier to read and maintain.



      Here's a pseudo-code-version of it:



      $all_fields = get_fields(); 
      foreach( $all_fields as $field ):
      if( $field == 'field_1' ):
      // Deal with field_1
      elseif( $field == 'field_2' ):
      // Deal with field_2
      elseif( $field == 'field_3' ):
      // Deal with field_3
      ...
      ... etc.
      ... etc.
      endif;
      endforeach;


      Now I'm facing a challenge that will require me to structure the code better; I will need another page-template (page-foobar.php), that includes the same ACF-code as shown above.



      On the one hand side, then I thought about making this whole thing into a (or several) functions and then sticking it in functions.php. But then I'm polluting the functions.php-file, which would suck.



      Ideally, I would do this 'the WordPress way', if there is such a thing for what I'm doing, but I haven't heard of such a thing. So if there isn't one, - then I'd prefer to have a folder in my theme called 'acf-modules', and then a file for each field, containing a function like this:



      function field_1( $field_information )
      // Deal with $field_information



      But should I then make an action in functions.php, requiring each file upon WordPress' init-hook? Is that really the cleanest/best way to do this?







      theme-development advanced-custom-fields






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 hours ago









      cjbj

      10.7k102866




      10.7k102866










      asked 3 hours ago









      Zeth

      13211




      13211




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Well, structuring is partly a matter of taste, but it in this case it seems to be a good idea to include all the functions in a separate file. That includes the code that you would have in both template files. And rather than the chain of elseif you could use PHP's switch statement. So you would have:



          In your functions.php (just plain, not depending on a hook)



          require_once (get_template_directory() . '/field-functions.php');


          In your template files



          deal_with_fields ();


          In your field-functions.php



          function deal_with_fields () 
          $all_fields = get_fields ();
          foreach ($all_fields as $field)
          switch ($field)
          case 'field_1' : deal_with_field_1 ($field); break;
          case 'field_2' : deal_with_field_2 ($field); break;
          ....




          function deal_with_field_1 ($field)
          do your thing;


          function deal_with_field_2 ($field) ....





          share|improve this answer




















            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "110"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f318318%2fsame-acf-on-two-different-pages%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            Well, structuring is partly a matter of taste, but it in this case it seems to be a good idea to include all the functions in a separate file. That includes the code that you would have in both template files. And rather than the chain of elseif you could use PHP's switch statement. So you would have:



            In your functions.php (just plain, not depending on a hook)



            require_once (get_template_directory() . '/field-functions.php');


            In your template files



            deal_with_fields ();


            In your field-functions.php



            function deal_with_fields () 
            $all_fields = get_fields ();
            foreach ($all_fields as $field)
            switch ($field)
            case 'field_1' : deal_with_field_1 ($field); break;
            case 'field_2' : deal_with_field_2 ($field); break;
            ....




            function deal_with_field_1 ($field)
            do your thing;


            function deal_with_field_2 ($field) ....





            share|improve this answer
























              up vote
              2
              down vote



              accepted










              Well, structuring is partly a matter of taste, but it in this case it seems to be a good idea to include all the functions in a separate file. That includes the code that you would have in both template files. And rather than the chain of elseif you could use PHP's switch statement. So you would have:



              In your functions.php (just plain, not depending on a hook)



              require_once (get_template_directory() . '/field-functions.php');


              In your template files



              deal_with_fields ();


              In your field-functions.php



              function deal_with_fields () 
              $all_fields = get_fields ();
              foreach ($all_fields as $field)
              switch ($field)
              case 'field_1' : deal_with_field_1 ($field); break;
              case 'field_2' : deal_with_field_2 ($field); break;
              ....




              function deal_with_field_1 ($field)
              do your thing;


              function deal_with_field_2 ($field) ....





              share|improve this answer






















                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                Well, structuring is partly a matter of taste, but it in this case it seems to be a good idea to include all the functions in a separate file. That includes the code that you would have in both template files. And rather than the chain of elseif you could use PHP's switch statement. So you would have:



                In your functions.php (just plain, not depending on a hook)



                require_once (get_template_directory() . '/field-functions.php');


                In your template files



                deal_with_fields ();


                In your field-functions.php



                function deal_with_fields () 
                $all_fields = get_fields ();
                foreach ($all_fields as $field)
                switch ($field)
                case 'field_1' : deal_with_field_1 ($field); break;
                case 'field_2' : deal_with_field_2 ($field); break;
                ....




                function deal_with_field_1 ($field)
                do your thing;


                function deal_with_field_2 ($field) ....





                share|improve this answer












                Well, structuring is partly a matter of taste, but it in this case it seems to be a good idea to include all the functions in a separate file. That includes the code that you would have in both template files. And rather than the chain of elseif you could use PHP's switch statement. So you would have:



                In your functions.php (just plain, not depending on a hook)



                require_once (get_template_directory() . '/field-functions.php');


                In your template files



                deal_with_fields ();


                In your field-functions.php



                function deal_with_fields () 
                $all_fields = get_fields ();
                foreach ($all_fields as $field)
                switch ($field)
                case 'field_1' : deal_with_field_1 ($field); break;
                case 'field_2' : deal_with_field_2 ($field); break;
                ....




                function deal_with_field_1 ($field)
                do your thing;


                function deal_with_field_2 ($field) ....






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 hours ago









                cjbj

                10.7k102866




                10.7k102866



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f318318%2fsame-acf-on-two-different-pages%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    Installing NextGIS Connect into QGIS 3?

                    One-line joke